Minimum Word Count Before A Post Can Be Made Pending Review

I’m looking for a way to check that a post has a minimum word count before allowing it to be sent for review (added to the pending post status).

Below is the code I have:

if (current_user_can('contributor')) {
function minWord($content){
        global $post;
        $num = 150; //set this to the minimum number of words
        $content = $post->post_content;
        if (str_word_count($content) <  $num)
            wp_die( __('Error: your post is below the minimum word count.') );
}
add_action('draft_to_pending', 'minWord');
}

This appears to “work”, in that it brings up an error message when the word count is indeed below 150. However, the post is still saved as a “pending”. Is there any way to prevent the post from being made pending, but instead keep it saved as “draft”.

Cheers

Peter

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Those transition post hooks run after the post is saved. You will have to interrupt the process earlier. I would hook to wp_insert_post_data.

function minWord($data){
  if (current_user_can('contributor')) {
    $num = 150; //set this to the minimum number of words
    if (str_word_count($data['post_content']) <  $num) {
      $data['post_status'] = 'draft';
    }
  }
  return $data;
}
add_action('wp_insert_post_data','minWord');

Seems to work when I test it. Following your code, this only effects the “Contributor” role. It does not interrupt status changes for other roles.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x