Halloween Sale 15% off Ends in See the plans
PHP

Minimum Word Count to Publish Posts

Stops posts shorter than a minimum number of words from being published, in both the block editor and the classic editor, to keep thin content off the site.

Don’t have WPIDE yet? Install it on your site

Code

// Minimum number of words a post needs before it can be published.
$wpide_min_words = 300;

$wpide_count_words = function ( $content ) {
    return count( preg_split( '/\s+/u', trim( wp_strip_all_tags( strip_shortcodes( (string) $content ) ) ), -1, PREG_SPLIT_NO_EMPTY ) );
};

add_filter( 'rest_pre_insert_post', function ( $prepared, $request ) use ( $wpide_min_words, $wpide_count_words ) {
    $post_id = isset( $prepared->ID ) ? (int) $prepared->ID : 0;
    $status  = isset( $prepared->post_status ) ? $prepared->post_status : ( $post_id ? get_post_status( $post_id ) : 'draft' );

    if ( ! in_array( $status, array( 'publish', 'future' ), true ) ) {
        return $prepared;
    }

    $content = isset( $prepared->post_content ) ? $prepared->post_content : ( $post_id ? get_post_field( 'post_content', $post_id ) : '' );

    if ( $wpide_count_words( $content ) < $wpide_min_words ) {
        return new WP_Error( 'wpide_too_short', sprintf( 'This post needs at least %d words before it can be published.', $wpide_min_words ), array( 'status' => 400 ) );
    }

    return $prepared;
}, 10, 2 );

add_filter( 'wp_insert_post_data', function ( $data ) use ( $wpide_min_words, $wpide_count_words ) {
    if ( ! is_admin() || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) || 'post' !== $data['post_type'] || 'publish' !== $data['post_status'] ) {
        return $data;
    }

    if ( $wpide_count_words( wp_unslash( $data['post_content'] ) ) < $wpide_min_words ) {
        $data['post_status'] = 'draft';

        add_filter( 'redirect_post_location', function ( $location ) {
            return add_query_arg( 'wpide_too_short', 1, remove_query_arg( 'message', $location ) );
        } );
    }

    return $data;
} );

add_action( 'admin_notices', function () use ( $wpide_min_words ) {
    if ( isset( $_GET['wpide_too_short'] ) ) {
        echo '<div class="notice notice-error"><p>' . esc_html( sprintf( 'The post was saved as a draft because it has fewer than %d words.', $wpide_min_words ) ) . '</p></div>';
    }
} );

Notes

Drafts can still be saved at any length. Change the minimum at the top of the snippet.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.