How to Set a Minimum Length for WordPress Posts

WP Engine
How to Set a Minimum Length for WordPress Posts

Let’s say you have a blog with multiple authors and want to  enforce certain standards with regard to post length. What you want to do is show them an error message after clicking “publish” or save it as a draft. One way to go about it is to use the suggestion of WPBeginner by implementing the “wp_die()” WordPress function. This causes a complete halt in the execution of the code and displays a blank screen with an error message that looks like this:

not a good message

While this certainly serves the bare purpose of letting the author know that the post is of insufficient length, it leaves a lot to be desired. Most importantly, it doesn’t let the user know whether or not there post contents have been lost. They’re forced to use the back button on the browser which is a scary thing to have to do if you’ve already written something! Moreover, it leaves the status of the post unclear to the author. Has it been deleted? Has it been unpublished? Has it been saved as a draft? What has happened?

Personally, I feel that an error message above the content of the post is more appropriate instead of redirecting the user to a new page with nothing but a warning. Moreover, the status of the buttons in the text editor after the message is displayed should make it clear that the post hasn’t been published, yet the author’s work is still saved as a draft. Basically, instead of seeing the traditional “Post Published” status message, the user should see the error message instead – with a nice red border to draw their attention to it.

Luckily for us, this entire solution is possible with a few simple lines of code.

Display an Error Message for a Short Post

As an arbitrary number, let’s assume that you want to disable the publishing of all posts that have a word count of less than 100 words. Open up your functions.php file or wherever else you write custom PHP code and paste in the following:

function display_short_error_message($messages)

global $post;
$content = $post->post_content;
if (str_word_count($content) < 100 ) {
$error_message = 'Post Should be at Least 100 Words';
add_settings_error('post_short_error', '', $error_message, 'error');
settings_errors( 'post_short_error' );
$post->post_status = 'draft';

return $messages;


add_action('post_updated_messages', 'display_short_error_message');

The first thing you may note is that we hooking into a strange action called “post_updated_messages”. This action is called when WordPress has to display status messages of posts transitions – whether from draft to published, draft to pending, or vice versa. This redirects to a function in which we simply extract the length of the post, and compare it to a minimum length.

Next, we use the “settings_errors” API to create an error message with the predefined class “error” that is automatically styled by WordPress in a certain manner. Finally, after displaying the message I force the post back to “draft” status regardless of what it was before. This way, when the page finishes rendering it will have the “Save Draft” and “Publish” buttons available so that the author can simply continue writing and publish their post after it has passed the minimum length.

Finally, I disable all other status messages if my own is displayed so that users don’t see conflicting messages. However if the post passes the minimum limit check, I simply parse the existing set of messages as usual. The end result looks like this:

error with saving

As you can see, this is a much more elegant way of displaying post errors as opposed to the “wp_die()” function. The author knows that there post is still safe and removes any confusion as to its published status.

One Reply to “How to Set a Minimum Length for WordPress Posts”!

Leave a Reply

Disclosure: We receive a compensation from some of the companies whose products are presented on our website.
%d bloggers like this: