0

How do I add a minimum word count to the Excerpt field before the post can be sent for pending review? I want to require content in the Excerpt field.

And also, how do I add the Excerpt field as a condition to the WyPiekacz plugin? WyPiekacz is a plugin that checks if posts that are submitted for review satisfies a set of rules.

1 Answer 1

0

Add minimum word count to Excerpt field

To enforce a minimum word count for the Excerpt field before the Post status can be changed to pending, we will slightly alter s_ha_dum's code.

It can be done by filtering wp_insert_post_data:

function wcexcerpt($data){
  if (!current_user_can('edit_others_posts')) {
    $num = 5; // minimum word count
    if (str_word_count($data['post_excerpt']) <  $num) {
      $data['post_status'] = 'draft';
    }
  }
  return $data;
}
add_action('wp_insert_post_data','wcexcerpt');

This will be active for all users beside the ones that can edit_others_posts.

Now, you will need to show an error message to the user if they fail the word count. You can do this by either:

Adding wp_die:

wp_die( __('Error: your excerpt is below the minimum word count.') );

Or via an Admin Notice:

function excerpt_error() {
    ?>
    <div class="error">
        <p><?php _e( 'Error: your excerpt is below the minimum word count.' ); ?></p>
    </div>
    <?php
}
add_action( 'admin_notices', 'excerpt_error' );

Add Excerpt as a rule to WyPiekacz

Now, this one is a bit more complicated. To enforce the Excerpt field as a condition for the WyPiekacz plugin, do the following steps.

Firstly, we will add the word count limit to the enforce_rules function.

Search for:

$add_dots = get_option( 'wypiekacz_enforce_add_dots' );

And add the following underneath that line:

// Enforce Excerpt for none-admins
if (!current_user_can('administrator')) {
  $num = 1; // minimum word count
   if (str_word_count($data['post_excerpt']) <  $num) {
      $this->errors[] = array( 'excerpt', __('Add an Excerpt to your post.', 'wypiekacz') );
   } 
}

By reading the code above, you will notice that we have added an error message to the errors array:

$this->errors[] = array( 'excerpt', __('Add an Excerpt to your post.', 'wypiekacz') );

Then let's add the option. Search for:

add_option( 'wypiekacz_post_thumbnail', 0 ); // Check post thumbnail

And add the following underneath that line:

add_option( 'wypiekacz_excerpt', 1 ); // Check excerpt

Finally, let us register setting. Search for:

register_setting( 'wypiekacz', 'wypiekacz_post_thumbnail', array( &$this, 'sanitize_01' ) );

And add the following underneath that line:

register_setting( 'wypiekacz', 'wypiekacz_excerpt', array( &$this, 'sanitize_nonnegative' ) );

That's it! Now you can force your authors to add an Excerpt to your post.

Want to add a maximum word count too? Sure, use Rarst's solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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