以匿名用户发送Buddypress/Buddyboss消息的问题



我创建了一个自定义函数,在Buddyboss/Buddypress中新编写的私人消息末尾添加一个复选框,允许用户匿名发送消息。

如果复选框被选中,那么发件人id应该是55,因为我已经创建了一个名为Anonymous的用户,其userID为55。如果复选框被选中,我希望消息显示为好像它们是从该用户发送的。

代码如下:

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 );

我已经成功地添加了这个函数,但是它似乎没有执行。我没有得到任何错误,它只是永远不会发送消息,也不会在我点击发送按钮后刷新。我相信这可能与我在最后添加的过滤器有关。我错过什么了吗?

messages_message_before_save不是一个过滤器钩子,尽管它可以用来过滤参数。

do_action_ref_array( 'messages_message_before_save', array( &$this ) );

检查钩子

的上下文评论do_action_ref_array

最新更新