Recently, one of our users asked us how they can add a list of forbidden words for WordPress post titles.
This might sound like a niche feature, but it can be incredibly useful for anyone managing a multi-author WordPress blog. By creating a list of forbidden words or phrases, you can prevent authors from publishing posts with titles you don’t approve of.
In this article, we’ll show you how to create a list of forbidden words for WordPress titles, giving you greater control over your content.
Why Create a List of Forbidden Words for Post Titles in WordPress?
It is not easy to keep all authors informed about your editorial style and policy on a multi-author site. You can use a WordPress plugin to leave editorial comments, add notes, and custom statuses, but it will not monitor your post titles.
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.
Having said that, let’s see how you can easily add a list of banned words for WordPress post titles.
Adding a List of Banned Words for WordPress Post Titles
This method requires you to manually add code to your WordPress site. It is recommended for users who know how to paste code snippets from web into WordPress.
Important: Always backup your WordPress site when you are adding a code snippet to your WordPress files.
Simply add the following code to your theme’s functions.php file, in a site-specific plugin, or in a code snippets plugin.
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);
Note: Don’t forget to add the words you want to ban in $restricted_words
variable. You need to use a semicolon to separate different words and phrases.
We recommend adding this code snippet using WPCode, the best WordPress code snippets plugin.
WPCode makes it safe and easy to add custom code in WordPress, without editing your theme’s functions.php file. It comes with a library of popular code snippets, smart code validation, conditional logic, and more.
First, you need to install and activate the free WPCode plugin. For instructions, see this guide on how to install a WordPress plugin.
Once the plugin is activated, head to the Code Snippets » + Add Snippet page from the WordPress dashboard.
Then, click the ‘Use Snippet’ button under the ‘Add Your Custom Code (New Snippet)’ option.
Next, enter a title for your snippet and paste the code from above into the ‘Code Preview’ box.
Don’t forget to select ‘PHP Snippet’ as the code type from the dropdown menu on the right.
After that, simply toggle the switch from ‘Inactive’ to ‘Active’ and click the ‘Save Snippet’ button.
Now the code will trigger 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 like this:
That’s all, we hope this article helped you learn how to add a list of forbidden words for WordPress post titles. You may also want to see our guide on how to require featured images for posts in WordPress or our expert picks of the best popular posts plugins for WordPress.
If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.
Syed Balkhi
Hey WPBeginner readers,
Did you know you can win exciting prizes by commenting on WPBeginner?
Every month, our top blog commenters will win HUGE rewards, including premium WordPress plugin licenses and cash prizes.
You can get more details about the contest from here.
Start sharing your thoughts below to stand a chance to win!
Dennis Muthomi
I’ve implemented this and it’s been a lifesaver. One more tip: consider creating a shared doc with your authors explaining why the words are forbidden. That way everyone will understand and you won’t have to do as many post edits after the fact. Thanks for sharing!
petri
Excellent, simple example. How should I modify this if I would want to make same for post content. More better if I could define post type
charles langat
Is possible to modify the code above use it to show censored words in the wordpress posts in the text area?
venkat
Hi ..in my wordpress site..i uploaded .sql files through ftp
when i open that .sql file in separate page,files are forbidden…getting an error like 403 forbideen error…how can i access those files…
pls help me out