How can you make sure authors’ posts are longer than 250 words?

Prevent authors from publishing too short content:

Here’s one idea using a custom post status, for example short:

/**
 * Register a custom 'short' post status
 *
 * @see http://wordpress.stackexchange.com/a/159044/26350
 */

function wpse_short_post_status()
{
    register_post_status( 'short', array(
        'label'                     => _x( 'Short', 'post' ),
        'public'                    => false,
        'exclude_from_search'       => true,
        'show_in_admin_all_list'    => true,
        'show_in_admin_status_list' => true,
        'label_count'               => _n_noop( 'Short <span class="count">(%s)</span>', 
                                                'Short <span class="count">(%s)</span>' )
    ) );
}

add_action( 'init', 'wpse_short_post_status' );

We can then view all posts with too short content here:

/wp-admin/edit.php?post_status=short&post_type=post

with the extra tab:

Short post status

To prevent authors from publishing posts with too short content, we can use the
wp_insert_post_data filter:

/**
 * Prevent authors from publishing posts with too short content.
 *
 * @see http://wordpress.stackexchange.com/a/159044/26350
 */

function wpse_prevent_short_content( $data , $postarr )
{
    // Editors and admins can publish all posts:
    if( current_user_can( 'edit_others_posts' ) )
        return $data;   

    // Authors can't publish posts with too short content: 
    $wordcount = count( explode( ' ', strip_tags( $data['post_content'] ) ) );

    if( 'publish' === $data['post_status'] && $wordcount <= 250 )
        $data['post_status'] = 'short';

    return $data;
}

add_filter( 'wp_insert_post_data', 'wpse_prevent_short_content', PHP_INT_MAX, 2 );

where we force the post status back to short on publish.

We can use this to warn the user that the content is too short:

/**
 * Display a too short content warning.
 *
 * @see http://wordpress.stackexchange.com/a/159044/26350
 */

function wpse_admin_notice() {
    $screen = get_current_screen();

    if(    'post' === $screen->base
        && 'post' === $screen->id
        && 'short' === $GLOBALS['post']->post_status
        && ! current_user_can( 'edit_others_posts' )
    )
    { 
    printf( '<div class="error"><p>%s</p></div><style>#message{display:none;}</style>',
        __( 'Warning: Post not published - the content must exceed 250 words!' )
    );
    }
}

add_action( 'admin_notices', 'wpse_admin_notice' );

Here’s a screenshot of the warning:

Warning


I hope you can modify this to your needs, for example if you need this for other post types than post.

Leave a Comment