Beginner's Guide for WordPress - Start your WordPress Blog in minutes.
Choosing the Best
WordPress Hosting
How to Easily
Install WordPress
Recommended
WordPress Plugins
View all Guides

How to Set a Minimum Word Count for WordPress Posts

Last updated on by
Special WordPress Hosting offer for WPBeginner Readers
How to Set a Minimum Word Count for WordPress Posts

Do you run a blog with multiple authors? Then, you probably have wondered how you can set a minimum word count for your posts in WordPress. In this article, we will share with you a snippet that lets you set a minimum Word count for your WordPress posts. If a user tries to publish a post that is too small, then it will return an error telling them the post is not long enough.

Simply open your theme’s functions.php file and paste the following code:

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. It needs to be longer than 100 words.') );
}
add_action('publish_post', 'minWord');

You may change the minimum number of words from 100 to whatever you like. You can also customize the error to make it helpful.


Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi. Page maintained by Syed Balkhi.

WPBeginner's Video Icon
Our HD-Quality tutorial videos for WordPress Beginners will teach you how to use WordPress to create and manage your own website in about an hour. Get started now »

Comments

  1. BoiteAWeb says:

    Hello
     
    No need to globalize $post since it’s passed in 2nd argument.
    You “$content” parameter is wrong, the 1st param is not the content but a post ID.
    So i modified the code to change this, i used ‘dummy’ to understand we do not need it in you function.
     
    function minWord( $postID_dummy, $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. It needs to be longer than 100 words.’) );
    }
    add_action( ‘publish_post’, ‘minWord’, 10, 2 );
     
    Core : http://core.trac.wordpress.org/browser/trunk/wp-includes/post.php#L2980
     
    See you !
     
    Julio – WordPress Expert.

  2. Antillais says:

    do you think this can work this gravity form plugin ?

  3. Frank Mulder says:

    Wohaa! I’ve been looking for this for a long time! Tnx!

  4. Jeff Bridgforth says:

    I assume you could edit this script and set it up for a maximum word count as well. Is this correct?

  5. Brandon Cox says:

    Ah, cool little trick! I can see it coming in handy on those mega-magazine blogs everyone wants to build.

  6. Mark Morgan says:

    Is there a way to do that with comments, too? A minimum comment size might improve the quality of them.

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.