Halloween Sale 15% off Ends in See the plans
PHP

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.

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

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

  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.