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.
Opens WPIDE on
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
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.