If you manage a multi-author blog and want authors to avoid using certain words or phrases, then this will come in handy. If an author has publishing rights, then the unwanted words can go live on your website. You can prevent this by taking away publishing privileges from users, but this means more work for you as you will have to review and publish posts yourself. So in this tutorial, we will show you how to create a list of forbidden words for WordPress titles.
Simply add the following code to your theme’s functions.php
file like in previous tutorials, or use a site-specific plugin.
function itlc_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', 'itlc_forbidden_title', 10, 1);
Don’t forget to add the words you want to ban in the $restricted_words
variable. You need to use a semicolon to separate different words and phrases.
This code simply triggers a function when a user tries to publish a post which checks the post title for restricted words. If it finds a restricted word in the post title, then it will show the user an error.