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.
hello,
it still works and is very useful. thank you.
how about if I want to show the error message instead the “post published. view post” message it shows when you press the publish button? or even better in a browser error popup
as it is, the script kills wp and displays the message in a new page, but still publishes the article.
chapeau
Thank you for letting us know about this, it could be due to updates to WordPress. We will certainly take a look at alternatives.
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.
do you think this can work this gravity form plugin ?
Wohaa! I’ve been looking for this for a long time! Tnx!
I assume you could edit this script and set it up for a maximum word count as well. Is this correct?
Yes you can do that as well.
Yea, don’t you just turn the bracket around ( more)
Ah, cool little trick! I can see it coming in handy on those mega-magazine blogs everyone wants to build.
Is there a way to do that with comments, too? A minimum comment size might improve the quality of them.