Warning Banner When Editing Key Pages
Shows a prominent warning at the top of the editor when someone opens an important page, such as the homepage, checkout or a landing page, reminding them to be careful.
Opens WPIDE on
Code
// Page ID => message.
$wpide_key_pages = array(
// 123 => 'This page is linked from every email we send. Check links after editing.',
);
$wpide_key_page_message = function () use ( $wpide_key_pages ) {
$post = get_post();
if ( ! $post || 'page' !== $post->post_type ) {
return '';
}
$pages = $wpide_key_pages;
if ( (int) get_option( 'page_on_front' ) ) {
$pages[ (int) get_option( 'page_on_front' ) ] = $pages[ (int) get_option( 'page_on_front' ) ] ?? 'This is the homepage. Changes are visible to every visitor immediately after updating.';
}
if ( (int) get_option( 'wp_page_for_privacy_policy' ) ) {
$pages[ (int) get_option( 'wp_page_for_privacy_policy' ) ] = $pages[ (int) get_option( 'wp_page_for_privacy_policy' ) ] ?? 'This is the privacy policy. Legal review may be required for changes.';
}
return $pages[ $post->ID ] ?? '';
};
// Classic editor.
add_action( 'admin_notices', function () use ( $wpide_key_page_message ) {
$screen = get_current_screen();
$message = $screen && 'post' === $screen->base ? $wpide_key_page_message() : '';
if ( $message ) {
printf( '<div class="notice notice-warning"><p><strong>Careful:</strong> %s</p></div>', esc_html( $message ) );
}
} );
// Block editor.
add_action( 'enqueue_block_editor_assets', function () use ( $wpide_key_page_message ) {
$message = $wpide_key_page_message();
if ( ! $message ) {
return;
}
wp_add_inline_script(
'wp-notices',
sprintf(
'wp.domReady(function(){wp.data.dispatch("core/notices").createNotice("warning",%s,{isDismissible:false,id:"wpide-key-page"});});',
wp_json_encode( 'Careful: ' . $message )
),
'after'
);
wp_enqueue_script( 'wp-notices' );
wp_enqueue_script( 'wp-dom-ready' );
} );
Notes
The homepage and privacy page are included automatically; add more page IDs and messages to $wpide_key_pages.
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.