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 making a website that authors can sign up to and write articles. However I don't want low quality posts submitted. So, firstly I want the posts to be not empty and to be longer than 250 words.

So something like:

if (empty($post) && $post < 250) {
 echo 'Error, must be longer than 250 words';
}

Obviously this code will not work but that is a general idea of what I want. However, I'm not really sure how to go about this. Is their a WordPress hook to use?

share|improve this question
    
So you just want to display a warning for the contributors? Since they can't publish the posts anyway, I guess it's up to you to check if the #words > 250 before publishing. –  birgire Aug 25 at 12:58
    
Sorry, I accidentally wrote contributors however I meant authors. I would like to physically stop them from posting if the requirements are not met. –  chap Aug 25 at 13:03

1 Answer 1

up vote 6 down vote accepted

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.

share|improve this answer
    
Working nicely so far. –  chap Aug 25 at 13:36
    
Great, I just got an idea using a custom post status for posts with too short content, for example short, let me try to implement it. –  birgire Aug 25 at 14:14
    
This is working really well. However, as well as the custom error message it is also display a success message saying post published. I can't find a filter to remove this. Is there a way to do that? –  chap Aug 26 at 2:03
1  
Please check the updated answer. –  birgire Aug 26 at 3:50

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.