Minimum Word Count for WordPress Posts
If you wish to set a minimum word count for your posts (this can be useful for a variety of things, including maintaining a clean layout), simply add the following snippet to your functions.php file and set the number to whatever you wish.
1 2 3 4 5 6 7 8 |
function minWord($content) { global $post; $content = $post->post_content; if (str_word_count($content) < 100 ) //set this to the minimum number of words wp_die( __('Error: your post is below the minimum word count.') ); } add_action('publish_post', 'minWord'); |
If an author tries to publish a post with fewer than the minimum, WordPress will kick out an error.
Nice and simple.










28 Comments
i cant get this done.
Hi Pippin, thank you very much for sharing this tip with us, I took time looking for something to perform this function. Unfortunately this does not work on my site, I have included these lines in my functions.php without results. In my case the user must complete a form on the site and I approve after the article in my control panel.
It is a real estate website and am very interested in forcing the user to write at least 200 words … Some come users are very lazy! If you have any suggestions I’ll be very grateful.
Is the form they use a custom built form? If so, then this will not work, though I’d be more than happy to help you make it work with your site. Just email me.
Can you explain more? Does it throw an error, does it not seem to work, etc?
can i define maximum TITLE word count?
Sure just do it like this:
function maxWord($title)
{
global $post;
$title = $post->post_title;
if (str_word_count($title) >= 10 ) //set this to the maximumnumber of words
wp_die( __('Error: your post title is over the maximum word count.') );
}
add_action('publish_post', 'maxWord');
That should work.
adding a $min_words variable to store value would be better and could you please explain how code works especially the last line.
Yes, it would be better. So what’s happening here, is that the content of the post is being read from a variable called $content, then it’s getting tested to see if it is less than 100 words long with the PHP function str_word_count(). And if that function returns as true, then we tell WordPress to kill the current process, which in this case is publishing the post. The very last line is called a “hook” and tell WordPress to run this function we have just written every time that a post is published.
add_action(‘publish_post’, ‘minWord’); the action will only be taken if minWord function returns true or wp_die() stop execution and the WordPress hooks can not be exectued ?
The action will be taken every time a post is published, meaning that the function minWord($content) will run every time, but wp_die() will only happen if the $content variable has less than 100 words.
Understood thanks for explaining
Where to add the “add_action(‘publish_post’, ‘minWord’);” part? I placed all your code in the functions.php file, but it was not work.
can i define maximum word count for post?
Yes, just change “if (str_word_count($content) < 100 )" to "if (str_word_count($content) > 100 )”. Notice the “>”.
Hi, I tried the code on my blog and it’s working. The only problem is when a user tries to publish a post with less than the minimum words required. An error message will appear, but when he tries to go back a page to continue his work, everything he previously typed is gone and he would have to redo everything again. Is there any way to avoid this? Thanks
Hi, i tried to use your function minWord, but it seems to not work for me. I get message that post has not enough words, but after returning to panel post is already publish. Any solutions?
Hmm, this may be caused by the latest version of WordPress. When originally written, you were able to click Back and have the content still there. I will try and post an updated tutorial over at Pippin’s Plugins.com sometime soon.
How are you returning to the panel, by clicking Back?
I tried a few methods of returning, by clicking back, by entering new URL, by refreshing with F5 and after i did this i always saw published post
Artur, I think it might have to do with the later version of WordPress. I will investigate a new method and post it over at Pippin’s Plugins.com
Pippin
Any update on your great script?
@Hikari – No, sorry.
Thanks for giving this great code!
But i want when using this code this, post is not publish but save it draft.
Thanks Again!
@Aminul – it works for drafts and published.
Yes I know!
I See This message “Error: your post is below the minimum word count” but post was going published, its not save as draft!
@Aminul – I will try and look into it
Excellent code, but i don’t use this code. Can you help me?
Thanks
What so you mean? Does it not work for you?