Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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