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.
Opens WPIDE on
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
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.