Reputation: 11
I have created a custom function that adds a checkbox to the end of newly composed private messages within Buddyboss/Buddypress that allows the user to send messages anonymously.
If the check box is ticked, then the sender id should be 55, as I've created a user named Anonymous with the userID of 55. I want messages to appear as if they were sent from that user if the checkbox is ticked.
Here is the code:
function bp_add_anonymous_checkbox() {
if ( bp_is_active( 'messages' ) ) {
// Only show the checkbox if the user is composing a private message
if ( bp_is_messages_component() && bp_is_current_action( 'compose' ) ) {
// Output the checkbox
?>
<p>
<label for="bp-anonymous-message">
<input type="checkbox" name="bp-anonymous-message" id="bp-anonymous-message" value="1">
Send this message anonymously
</label>
</p>
<?php
}
}
}
add_action( 'bp_after_messages_compose_content', 'bp_add_anonymous_checkbox' );
function bp_anonymous_message_handler( $recipients, $subject, $content, $date_sent ) {
// Check if the anonymous message checkbox was checked
if ( ! empty( $_POST['bp-anonymous-message'] ) ) {
// Set the sender of the message to the user with an ID of 55
$sender_id = 55;
} else {
// Otherwise, use the current user's ID as the sender
$sender_id = get_current_user_id();
}
// Send the message as usual
return messages_new_message( $recipients, $subject, $content, $date_sent, $sender_id );
}
add_filter( 'messages_message_before_save', 'bp_anonymous_message_handler', 10, 4 );
I have successfully been able to add the function, but it does not appear to execute. I don't get any error, it simply just never sends the message nor does it refresh after I've hit the send button. I believe it might related to the filter I've added at the end. Am I missing something?
Upvotes: 1
Views: 235
Reputation: 1956
messages_message_before_save
is not a filter hook, although it can be used to filter the args.
do_action_ref_array( 'messages_message_before_save', array( &$this ) );
Review the context of the hook
Upvotes: 0