Define minimum number of words for posts

When you start writing your first articles, you will be able to publish your posts no matter how many words or characters are in there. At first, you might think there is no reason for setting the minimum number of words for posts, but as you continue on your writing, you might change your mind.

For starters, your theme might be designed for bigger articles. While you won’t get any errors if you publish shorter articles, your site might actually look ridiculous. On the other hand, if you have more than one extra author writing for your site, you might have agreed on a minimum words per article. Instead of checking each and every article your authors write, you might want to automatize the process and forbid post publishing if the post hasn’t reach desired amount words.

Let’s get down to business. No matter what is the reason behind setting the minimum word count, here’s how you can easily do it.

Set minimum number of words for posts:

  1. Open functions.php
  2. Copy and paste this snippet:
  3. function minimum_words ($content){
    global $post;
    $num = 400; //you can change the number to any number of words
    $content = $post->post_content;
    if (str_word_count($content) <  $num)
    wp_die( __('WordPress won’t publish this post until there are at least ' . $num . ‘ words in it’) );
    }
    add_action('publish_post', 'minimum_words');
  4. Change the $num variable in the third line to any number of words you want to check for
  5. Save changes

It was easy as that. After you have made these changes, no author, including you, will be able to publish a post that isn’t at least $num words long. Yeah, if you try that, WordPress will leave you a message that you are also free to change on the 6th line of the code.

If you have made the change, you might want consider adding a warning notice above the post so that your authors know that the posts have been limited before they hit the publish button.

START YOUR OWN BLOG

This guide is an introduction to mastering the art of blogging. It provides easy to follow steps to start, maintain, and grow your blog.

Read the guide

Leave a Reply

Your email address will not be published. Required fields are marked *