How to Restrict a Specific Word from Post Title

Hooks are very useful in WordPress. They allow you to “hook” custom functions to an existing function, and allow you to modify WordPress’ functionality without editing the core files.

How to Restrict a Specific Word from Post Title

This article shows simple usage of WordPress hook. The hook we are using here is post_publish, to restrict a specific word from post title. You can read more about hook in WordPress here : link


function titlerestriction($title){
	global $post;
	$title = $post->post_title;
	$restrictedWord = "forbidden";
	if (stristr( $title, $restrictedWord)) 
	wp_die( __('Error: This title is containing restricted word') );
}
add_action('publish_post', 'titlerestriction');

Put the code in your function.php. And You can replace word “forbidden” with other word you want to restrict.

- image credit -

5 Comments to “How to Restrict a Specific Word from Post Title”

Add Comments (+)

  1. Was looking for this, thanks!

    I guess we can also use an array of words instead of just one, right?

    • reza says:

      sure, the code will be like this

      
      function titlerestriction($title){
      	global $post;
      	$title = $post->post_title;
      	$restrictedWords = "forbidden1;forbidden2;forbidden3";
      	$restrictedWords = explode(";", $restrictedWords);
      	foreach($restrictedWords as $restrictedWord){
      		if (stristr( $title, $restrictedWord)) 
      		wp_die( __('Error: This title is containing restricted word') );	
      	}
      
      }
      add_action('publish_post', 'titlerestriction');
      
      

      the array of restricted words here are forbidden1, forbidden2 and forbidden3
      We can add more words but should be separated with “;”
      just like the code above

  2. csabi says:

    Hi! Great tutorial!
    I`we just opened a tutorial indexing website, http://www.tutorialswindow.com and I would like to ask you to submit some tutorials if you want…

    Or if you let me I could submit some of your tutorials myself using my publishing system, you would need only to register and to send me a short “About the author” text.

    Thanks, you can contact me at info@tutorialswindow.com

    Please email me even if you don`t like my website, because I want to know what to improve on my website!

    Thanks
    Csabi

  3. mylife says:

    Hello,

    I am working on site in wordpress, where all the Jobs are pulled from the external jobs sites, such as monster, indeed, simplyhired. jobs are getting published on site randomly. but I need to explode the jobs titles after specific ascii character ” – ” for ex Sales and Marketing Manager – Mortons Steakhouse – San Francisco, CA and extract last jobs Address from the title and call it in location column.

    Can somebody give me php function for this as the ” – ” is common for all jobs titles published on site.

    waiting….

    Thanks

  4. Tommy says:

    Hi,

    Do you have a suggestion how to make this a plugin?

Trackbacks/Pingbacks

  1. How to Restrict a Specific Word from Post Title | The best Tutorials
  2. How to Restrict a Specific Word from Post Title | WebDevKungfu
  3. 50 Unique and Informative wordpress tutorial showcase - WordPress Vampire

Leave a Reply

 

Amazingly Beautiful WordPress Themes