How to Set a Minimum Word Count for Posts in WordPress Without a Plugin

How to Set Minimum Word Count for Posts in WordPress

If you search for the ideal minimum word count for website articles, you’ll find the advice varies between 350 words up to 2000 words. Primarily the advice is too towards Google search ranking algorithms but your mindset should be focused towards your visitors and their user experience by writing high-quality content.

With this in mind, we recommend you aim to achieve a minimum word count for your articles towards the higher end whilst keeping user readability in mind. If your article is lengthy but poorly written, you’ll find your visitors will become disinterested and click away from your website resulting in bad user experiences. Similarly, if your articles are short but well written, punchy and to the point, you’ll find your visitors will be engaged and stay on your website for longer resulting in more positive user experiences.

Now if you run a WordPress website with multi-authors, you’ll want to impose certain standards to ensure you maintain your website’s content quality. One standard you might want to impose on your multi-authors is having a minimum word count for each post. With WordPress, you can easily set a minimum word count for your posts using the functions.php file.

In this guide, we will show you how easily you can add a configurable minimum word count function into WordPress using your functions.php file. The function will display a customisable error message at the top of the admin post page warning of the minimum word count has not been reached, prevent the post from being published to your WordPress website and automatically save as a draft post.

IMPORTANT In this tutorial, we'll be directly editing WordPress theme files and we recommend that you create a child theme of your existing parent theme. By using a child theme you will be able to apply modifications without altering the parent theme files and ensuring any changes you make are kept following any parent theme updates.
TIP If you don't feel comfortable with editing the functions.php file directly, we would recommend you use the Code Snippets plugin. This plugin will enable you to easily add, manage and delete WordPress code snippets from your dashboard. To find out more about adding custom code snippets using a plugin see our tutorial How to Add Custom Code Snippets to WordPress with the Code Snippets Plugin for more information.

On WPBeginner, they wrote an article detailing how to add a minimum word count to WordPress which suggests using the wp_die() WordPress function. We have posted the WordPress function from their articles below.

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');

Their recommended code snippet using the wp_die() function works in a similar way to the PHP die() function. The main difference between them that the wp_die() will output a message in HTML to the user whiist stopping the function from continuing. Using that function will output the error page with a message similar to the example below.

Minimum Post Word Count WordPress

The main problem with using this custom code snippet is primarily focused on the user experience. Whilst the wp_die() function does serve the purpose of displaying an error message when the minimum word count has not been reached, it could imply to the user that the written post content has been lost when the error message is displayed. Also, it does not provide any instrucitons on how the user would return to the WordPress post admin page. The only way to return to the previous page from this error page is by using the back button on your browser.

By using the following custom code snippet to implement a minimum word count to your WordPress posts, it will have a positive effect on your content writers by improving their user experience. This function will display an error message to the user at the top of the WordPress post admin page and removes the ability to publish a post when the minimum word count threshold has not been reached. In the meantime, the post will continue to be saved in as a draft post. Once the minimum word count threshold has been reached the publish post function will be enabled automatically and the author will have the ability to publish the post. Once impletmented, the final result in your WordPress post admin screen will look similar to the image below.

Minimum Post Word Count Functions

To add a minimum word count for your WordPress posts, simply copy and paste the following code into your functions.php file.

IMPORTANT After, implementing this function it will change the visibility of published posts to draft should they use less than the minimum word threshold you set in the function.
// Minimum Word Length
function vpsb_minimum_word_message($messages) {

global $post;
$content = $post->post_content;
if (str_word_count($content) < 1000 ) {
$error_message = 'Error! Your post is below the minimum word length. It needs to be at least 1000 words.’;
add_settings_error('post_short_error', '', $error_message, 'error');
settings_errors( 'post_short_error' );
$post->post_status = 'draft';
wp_update_post($post);
return; }
return $messages; }

add_action('post_updated_messages', ‘vpsb_minimum_word_message’);

If you want to change the minimum word count, simply edit the number in this line if (str_word_count($content) < 1000 ) {. For example, if you wanted 500 as the minimum word count you would change the code to read if (str_word_count($content) < 500 ) {. If you want to customise the error message displayed, you simply need to change the $error_message = line. For example, if you want the error message to say, Error! Your post needs to be at least 1000 words in length, you simply need to edit the code to $error_message = 'Error! Your post needs to be at least 1000 words in length.’;.

TIP After, implementing this function be sure to check the visibility of your previously published posts as they may have change status to draft. This function works very well with our How to Add Post Word Count Stats tutorial which enables you to quickly see the total word count of each post directly in the post admin screen.

That’s it. You have now successfully set a minimum word count for your WordPress posts.

VPSBasics

VPSBasics

This guide was written by the VPS Basics editorial team, led by Gilberto Van Roosen. They are a unique blend of people who are dedicated to providing highly detailed, comprehensive and easy to follow tutorials, written in plain English. They specialise in tutorials for managing Linux servers, its software and WordPress.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.