0

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

2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.