• Resolved locker17

    (@locker17)


    On my buddypress, users can be marked as “might be a spammer” before they got deleted. I need to tell users which are in dialogue with a spammer – this might be a spammer, be careful what you write. Do you have a filter where i can insert such a warning on top of the send answer textfield?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Support Andrij Tkachenko

    (@andrijtkachenko)

    Hi,

    Yes — you can do exactly this with the better_messages_rest_thread_item filter. Set a threadInfo value on the thread and Better Messages renders it as a banner pinned to the top of the open conversation (the same slot used for context banners), so both people see the warning the whole time they’re reading and typing.

    Because the check runs on the server, you can base it on any flag you already store on the user. Drop this into your child theme’s functions.php (or a code-snippets plugin):

    add_filter( 'better_messages_rest_thread_item', 'bm_warn_about_spammer', 20, 5 );
    function bm_warn_about_spammer( $thread_item, $thread_id, $thread_type, $include_personal, $user_id ) {
    // Only personal 1-on-1 / group threads (not chat rooms)
    if ( $thread_type !== 'thread' ) {
    return $thread_item;
    }

    $me = Better_Messages()->functions->get_current_user_id();
    $members = Better_Messages()->functions->get_recipients_ids( $thread_id );

    $flagged = array();
    foreach ( $members as $member_id ) {
    if ( $member_id === $me || $member_id < 0 ) {
    continue; // skip myself and guest users
    }

    // ↓↓↓ Replace this line with your own "suspected spammer" check ↓↓↓
    if ( function_exists( 'bp_is_user_spammer' ) && bp_is_user_spammer( $member_id ) ) {
    $flagged[] = Better_Messages()->functions->get_name( $member_id );
    }
    }

    if ( empty( $flagged ) ) {
    return $thread_item;
    }

    $text = sprintf(
    '⚠️ %s may be a spammer. Please do not share any personal or payment details.',
    esc_html( implode( ', ', $flagged ) )
    );

    $banner = '<div style="background:#fff4e5;border:1px solid #ffb74d;color:#8a5300;padding:10px 14px;border-radius:8px;font-size:13px;line-height:1.4;">' . $text . '</div>';

    $thread_item['threadInfo'] = ( isset( $thread_item['threadInfo'] ) ? $thread_item['threadInfo'] : '' ) . $banner;

    return $thread_item;
    }


    A few notes:

    • The banner only appears for the other person — the flagged user never sees a warning about themselves.
    • It checks every other participant, so it also covers group conversations.
    • The one line to customise is the bp_is_user_spammer() check. That’s a good default for BuddyPress’s built-in spam flag, but if your “might be a spammer” state is your own marker, just swap it for your own condition, e.g. get_user_meta( $member_id, 'your_maybe_spammer_key', true ).
    Thread Starter locker17

    (@locker17)

    Thanks for the quick answer.
    The solution you provided is very nice and saves me a lot of code lines but it has one caveat.
    On my page the admin can delete the warning of a user (if made by mistake etc.) but using your code doesnt update this. It still shows the warning notice and doesnt get a refresh.

    So I can’t delete the warning for a user once made.

    Plugin Support Andrij Tkachenko

    (@andrijtkachenko)

    That’s a caching effect — the original code only set threadInfo when a user was flagged, so once the banner was cached there was nothing to overwrite it when you removed the flag. The fix is to always set threadInfo (empty when there’s nothing to show). Here’s the updated snippet:

    add_filter( 'better_messages_rest_thread_item', 'bm_warn_about_spammer', 20, 5 );
    function bm_warn_about_spammer( $thread_item, $thread_id, $thread_type, $include_personal, $user_id ) {
    // Only personal 1-on-1 / group message threads (not chat rooms)
    if ( $thread_type !== 'thread' ) {
    return $thread_item;
    }

    $me = Better_Messages()->functions->get_current_user_id();
    $members = Better_Messages()->functions->get_recipients_ids( $thread_id );

    $flagged = array();
    foreach ( $members as $member_id ) {
    if ( $member_id === $me || $member_id < 0 ) {
    continue; // skip myself and guest users
    }

    // ↓↓↓ Replace this line with your own "suspected spammer" check ↓↓↓
    if ( function_exists( 'bp_is_user_spammer' ) && bp_is_user_spammer( $member_id ) ) {
    $flagged[] = Better_Messages()->functions->get_name( $member_id );
    }
    }

    $banner = '';
    if ( ! empty( $flagged ) ) {
    $text = sprintf(
    '⚠️ %s may be a spammer. Please do not share any personal or payment details.',
    esc_html( implode( ', ', $flagged ) )
    );
    $banner = '<div style="background:#fff4e5;border:1px solid #ffb74d;color:#8a5300;padding:10px 14px;border-radius:8px;font-size:13px;line-height:1.4;">' . $text . '</div>';
    }

    // Always set threadInfo (empty when not flagged) so removing the flag clears the banner
    $thread_item['threadInfo'] = ( isset( $thread_item['threadInfo'] ) ? $thread_item['threadInfo'] : '' ) . $banner;

    return $thread_item;
    }

    After this, when you remove the flag the warning goes away on the next refresh instead of sticking.

Viewing 3 replies - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.