Halloween Sale 15% off Ends in See the plans
PHP

Block Publishing Posts That Contain Placeholder Text

Refuses to publish content that still contains placeholder words such as "Lorem ipsum", "TODO", "TK" or "[insert", so unfinished drafts never go live by accident.

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

Code

// Phrases that mean a post is not finished.
$wpide_placeholder_phrases = array( 'lorem ipsum', 'todo', 'tk tk', '[insert', 'xxx', 'placeholder' );

$wpide_find_placeholder = function ( $text ) use ( $wpide_placeholder_phrases ) {
    $text = wp_strip_all_tags( (string) $text );
    foreach ( $wpide_placeholder_phrases as $phrase ) {
        if ( preg_match( '/(^|\W)' . preg_quote( $phrase, '/' ) . '($|\W)/i', $text ) ) {
            return $phrase;
        }
    }
    return '';
};

add_filter( 'rest_pre_insert_post', function ( $prepared, $request ) use ( $wpide_find_placeholder ) {
    $status = $request->get_param( 'status' ) ? $request->get_param( 'status' ) : get_post_status( (int) ( $prepared->ID ?? 0 ) );
    if ( ! in_array( $status, array( 'publish', 'future' ), true ) ) {
        return $prepared;
    }

    $content = $prepared->post_content ?? get_post_field( 'post_content', (int) ( $prepared->ID ?? 0 ), 'raw' );
    $found   = $wpide_find_placeholder( ( $prepared->post_title ?? '' ) . ' ' . $content );

    return $found
        ? new WP_Error( 'wpide_placeholder_text', sprintf( 'This post still contains placeholder text ("%s"). Replace it before publishing.', $found ), array( 'status' => 400 ) )
        : $prepared;
}, 10, 2 );

add_filter( 'wp_insert_post_data', function ( $data ) use ( $wpide_find_placeholder ) {
    if ( wp_is_serving_rest_request() || ! in_array( $data['post_status'], array( 'publish', 'future' ), true ) || ! in_array( $data['post_type'], array( 'post', 'page' ), true ) ) {
        return $data;
    }
    if ( $wpide_find_placeholder( wp_unslash( $data['post_title'] . ' ' . $data['post_content'] ) ) ) {
        $data['post_status'] = 'draft';
    }
    return $data;
} );

Notes

Add or remove phrases in $wpide_placeholder_phrases; matching ignores letter case.

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.