Write a list of words which can’t be used in post titles

While you can relatively easy limit the length of a title in WordPress, sometimes you will need to modify titles even more. Limiting the number of characters in a post title can help you maintain a stable design and it can help with SEO, but your authors would still be able to write anything they want.

Depending on what you write or on your partnership with some other company, you might have some words or phrases that you want to avoid in your titles. Those might be profanity words that you want to avoid or simply a brand name or two that you don’t want to be advertised on your site.

While you can sit and talk with your authors about that and ask them not mention words which might harm your site’s reputation, it is just the matter of time when one of the authors will forget all about it and publish a title which can make you look bad or even lose money by breaching the deal with a partner.

Instead of a verbal warning, why wouldn’t you write a function which will forbid anyone on your site to write a title with specific words? Or even better, why wouldn’t you just copy and paste the same function from below and simply change the words which you want to block?

Now that you’ve decide to remove specific words from post titles, let’s see how you can do that.

Remove specific words from titles:

  1. Open functions.php file
  2. Copy and paste the code:
  3. function titlerestriction($title){
    global $post;
    $title = $post->post_title;
    $restrictedWords = "word1;word2;word3";
    $restrictedWords = explode(";", $restrictedWords);
    foreach($restrictedWords as $restrictedWord){
    if (stristr( $title, $restrictedWord))
    wp_die( __('Error: You have used a forbidden word in post title') );
    }
    }
    add_action('publish_post', 'titlerestriction');
    
  4. Change words on the 4th Add as many as you like but don’t forget to separate them with semicolon
  5. Customize the error message on the 8th line
  6. Save changes

If you open a new post and try to publish it while the title contains one of the words you have specified in the code, WordPress will stop you from publishing the post and warn you with the message.

Although you may have told your authors about forbidden words, it wouldn’t hurt to show those words right below the title and write them as a reminder.

If it’s not a top (or bottom) secret, can you tell us which words have you put on the list and why?

START YOUR OWN BLOG

This guide is an introduction to mastering the art of blogging. It provides easy to follow steps to start, maintain, and grow your blog.

Read the guide

One thought on “Write a list of words which can’t be used in post titles

  1. Hey

    Nice idea, you could also do this by writing the words in a text file, and reading that. That way you don’t need to manually edit the code all the time you want to add a new word.

    Or alternatively, you can add a text box to one of the settings pages and read it from there.

    Regards,
    Andor Nagy

Leave a Reply

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