Blog

Minimum Word Count for WordPress Posts

Tutorials, Wordpress, WordPress Tutorials28 comments

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
  1. Azah says:

    i cant get this done.

  2. Vicente says:

    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.

  3. 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.

  4. Can you explain more? Does it throw an error, does it not seem to work, etc?

  5. rami says:

    can i define maximum TITLE word count?

  6. 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.

  7. Sonu says:

    adding a $min_words variable to store value would be better and could you please explain how code works especially the last line.

  8. 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.

  9. Sonu says:

    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 ?

  10. 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.

  11. Sonu says:

    Understood thanks for explaining :)

  12. Avery says:

    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.

  13. Sushyant says:

    can i define maximum word count for post?

  14. Yes, just change “if (str_word_count($content) < 100 )" to "if (str_word_count($content) > 100 )”. Notice the “>”.

  15. Akira says:

    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

  16. Artur says:

    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?

  17. 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.

  18. How are you returning to the panel, by clicking Back?

  19. Artur says:

    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

  20. 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

  21. Hikari says:

    Pippin
    Any update on your great script?

  22. @Hikari – No, sorry.

  23. Aminul says:

    Thanks for giving this great code!

    But i want when using this code this, post is not publish but save it draft.

    Thanks Again!

  24. @Aminul – it works for drafts and published.

  25. Aminul says:

    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!

  26. @Aminul – I will try and look into it

  27. Rubel says:

    Excellent code, but i don’t use this code. Can you help me?
    Thanks

  28. What so you mean? Does it not work for you?

Leave a Reply