Halloween Sale 15% off Ends in See the plans
PHP

bbPress: Reject Posts With Blocked Words

Refuses forum topics and replies that contain words or phrases from your blocklist, showing the member a polite error instead of publishing them.

Don’t have WPIDE yet? Install it on your site

Code

if ( ! function_exists( 'bbp_add_error' ) ) {
    return;
}

// Words or phrases to block.
$wpide_bbp_blocked = array( 'casino', 'cheap pills', 'crypto giveaway' );

$wpide_bbp_scan = function ( $text ) use ( $wpide_bbp_blocked ) {
    if ( current_user_can( 'moderate' ) ) {
        return $text;
    }

    $plain = wp_strip_all_tags( (string) $text );

    foreach ( $wpide_bbp_blocked as $word ) {
        if ( preg_match( '/\b' . preg_quote( $word, '/' ) . '\b/iu', $plain ) ) {
            bbp_add_error( 'wpide_bbp_blocked', '<strong>Error:</strong> Your post contains words that are not allowed in these forums.' );
            break;
        }
    }

    return $text;
};

add_filter( 'bbp_new_topic_pre_title', $wpide_bbp_scan );
add_filter( 'bbp_new_topic_pre_content', $wpide_bbp_scan );
add_filter( 'bbp_new_reply_pre_content', $wpide_bbp_scan );

Notes

Matching ignores case and checks both the title and the content. Moderators are not checked.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.