How to Create a List of Forbidden Words for WordPress Titles

For any blog owner, the good management of their blog should be the primary concern. If you are only one person running the blog then there should be no problem. But if you are running a multi-author blog where multiple authors publish articles and posts then you may need some safety precautions. Simply creating a list of forbidden words or phrases for authors, can be one of the useful precautions that you can imply to your blog. In this article we will discuss how to create a list of forbidden words for WordPress titles.

Back Up your files

Before making any changes it will be wise step to back up your theme files. We have tons of articles showing you how to back up your file.

How to Create a List of Forbidden Words for WordPress Titles

This is actually very simple. All you have to do is to manually insert few lines of codes to your theme’s functions.php file. If you are wondering where to find functions.php file, then login to your WordPress admin dashboard and go to Appearance > Editor. This will open the theme editor. On the right side you will find the list of all the theme’s files. Select Theme Funnctions file( functions.php) from the right and then place the following code separately at the very end of the file.


function banned_titles($The_Title)
{
    $BannedWords = "word1,word2,word3";  /* Insert your banned words or phrase seperated by comma. */
    $BannedWords = explode(",", $BannedWords);
     
    global $post;
    $The_Title = $post->post_title;
     
    foreach($BannedWords as $BannedWord)
    {
        if (stristr($The_Title, $BannedWord))
            wp_die( __('Error: please remove the forbidden word "'. $BannedWord .'" in post title first!') );
    }
}

add_action('publish_post', 'banned_titles', 10, 1);

The most important thing to remember in this code is to insert the words or phrases you want to ban. Replace the word1, word2 and word3 with the words or phase you want to ban and separate the words by comma. Finally don’t forget to save the file.

Also Read:   Disable Emojicons on Your WordPress Site

How to Create a List of Forbidden Words for WordPress Titles

Here is the quick explanation of the code. Whenever any post is published in your website, the function banned_titles will be called. The list of banned words are stored in the $BannedWords variable. The title of the post is compared with each of the words stored in $BannedWords variable using the PHP function stristr() . This is a Boolean function that will return true if the banned word is found in the title and returns false if it is not found. If the function returns true or if the banned word is found then the WordPress Execution will be terminated and error message will be generated.

Conclusion:

There are many things you can do to safeguard your blog. You can also remove the publishing privileges from other authors to avoid unwanted words to go live in your website. You can enforce the authors to follow the certain editorial style and the policy of your site, add notes and leave editorial comments. We hope this article on how to create a list of forbidden words for WordPress titles was helpful. You can also read our other article on “How to remove spam from WordPress comments”.

The following two tabs change content below.
Kantiman Bajracharya

Kantiman Bajracharya

Kantiman Bajracharya is a freelance web developer. He is a computer geek who has a Bachelor’s degree in Computer Engineering. He has earned lots of satisfied customers while working for numerous successful projects. He is also a WordPress theme creator and web article writer. On free time he likes reading philosophy books, traveling and playing chess.

Share This Post