How To Create a List of Forbidden Words for WordPress Titles

How to Create a list of Forbidden Words for WordPress Tittle

Do you want to create a list of forbidden words for WordPress titles? As an owner of a blog or a website, you have to manage your website well. At some point in your blogging career, you might have realized the need of creating a list of forbidden words for WordPress titles. This is a must-do, especially if you manage a multi-author blog and want authors to avoid using certain words or phrases in the titles.

So, let’s first see why you should create a list of forbidden words for WordPress titles.


Why Create a List of Forbidden Words for Post Titles in WordPress? 


It is quite a difficult task to keep all the authors of your website informed of your editorial style and policy. You can surely warn your authors about the policy of your blog, leave editorial comments, add notes or custom statuses, but these will not monitor all the contents.

If an author has publishing rights, then the unwanted words can go live on your website at any time. As a result, you will have to review all the contents manually again, which means more work for you. So, the only solution here is to create a list of forbidden words for WordPress titles.


Create a List of Forbidden Words for WordPress Titles 


You need to manually add code to your WordPress website to add a list of forbidden words for WordPress titles.

It is recommended to backup your WordPress website when you’re adding a code snippet to your WordPress files.

First of all, add the following snippet of code to the bottommost of your theme’s functions.php file.

function wpb_forbidden_title($title){
global $post;
$title = $post->post_title;

// Add restricted words or phrases separated by a 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 post title') );
}
}
add_action('publish_post', 'wpb_forbidden_title', 10, 1); 

Now, add the words that you want to ban from your WordPress website in the $resticted_words variable. You need to use a semicolon to separate the words and phrases.

You can now save the files and refresh your website.

This snippet of code just triggers a little function. A condition has kept in the “foreach” loop that checks the post title for restricted words when an author tries to publish a post.

If it finds a restricted word in the post title, then it will show the user an error like this:

bad word error in WordPress title

Well, that’s all there is. You can now successfully create a list of forbidden words for WordPress titles without any difficulty.

Have anything to ask, share or add? Feel free to use the comments section below.

Leave a Reply

Your email address will not be published. Required fields are marked *