Family Encyclopedia >> Electronics

How to Block Banned Words in WordPress Post Titles: Expert Code Guide

Recently, one of our users asked how to enforce a list of banned words in WordPress post titles. If you manage a multi-author blog and want to ensure authors avoid specific words or phrases, this proven method will help. As seasoned WordPress experts who've optimized dozens of sites, we'll walk you through creating a banned words list for post titles.

How to Block Banned Words in WordPress Post Titles: Expert Code Guide

Why Block Banned Words in WordPress Post Titles?

Managing editorial standards across multiple authors is challenging. Plugins like Edit Flow allow comments, notes, and custom statuses, but they don't control titles. If authors have publishing rights, unwanted words can slip through, harming your site's professionalism.

Revoking publish privileges shifts the burden to you for reviews and approvals. Instead, use this lightweight code solution we've tested on live production sites to maintain quality effortlessly.

Implementing a Banned Words List for WordPress Titles

This requires adding PHP code to your site—ideal for users comfortable with theme edits. Important: Always back up your WordPress site before modifying files.

Add the following snippet to your theme's functions.php file or a site-specific plugin:

function wpb_forbidden_title( $title ) {
    global $post;
    $title = $post->post_title;
    // Add restricted words or phrases separated by semicolon
    $restricted_words = "word1;word2;word3";
    $restricted_words = explode( ";", $restricted_words );
    foreach( $restricted_words as $restricted_word ) {
        if ( stristr( $title, $restricted_word ) ) {
            wp_die( __('Error: You have used a forbidden word "'.$restricted_word.'" in the post title.') );
        }
    }
}
add_action( 'publish_post', 'wpb_forbidden_title', 10, 1 );

Customize the $restricted_words variable with your banned terms, separated by semicolons. This function runs on post publish, scans the title, and blocks publication with an error if a banned word is detected:

How to Block Banned Words in WordPress Post Titles: Expert Code Guide

That's it! This reliable approach has helped our clients enforce title standards without extra hassle. For more, see our guide on requesting featured images in WordPress.

If this was useful, subscribe to our WordPress YouTube channel for video tutorials. Follow us on Twitter and Facebook too.