Admin & Dashboard PHP Beginner

Warn Editors When They Edit the Homepage

Show a warning at the top of the editor when someone opens the page set as your homepage.

Tested on WordPress 7.1 PHP 8.3 No plugin needed Updated
functions.php 22 lines
function nahnu_editing_front_page(): bool {
	$id = absint( $_GET['post'] ?? 0 );
	return $id && $id === (int) get_option( 'page_on_front' );
}

// Block editor.
add_action( 'enqueue_block_editor_assets', function (): void {
	if ( nahnu_editing_front_page() ) {
		wp_add_inline_script(
			'wp-notices',
			"wp.data.dispatch( 'core/notices' ).createWarningNotice( 'You are editing the homepage. Changes appear on the front page as soon as you update.', { isDismissible: true } );"
		);
	}
} );

// Classic editor.
add_action( 'admin_notices', function (): void {
	$screen = get_current_screen();
	if ( $screen && 'post' === $screen->base && ! $screen->is_block_editor() && nahnu_editing_front_page() ) {
		echo '<div class="notice notice-warning"><p><strong>You are editing the homepage.</strong> Changes appear on the front page as soon as you update.</p></div>';
	}
} );

Where to add this

Recommended A code snippets plugin Survives theme changes and is easy to switch off.
Child theme At the end of your child theme's functions.php file.
Must-use plugin As a .php file in wp-content/mu-plugins, starting with <?php.

Always add PHP to a staging site first, or have FTP or file manager access ready, so you can undo it if something goes wrong.

The homepage is the most visible page on a site and the easiest to break. A clear warning when someone opens it makes them slow down before clicking Update.

How it works

The page set under Settings → Reading as “Your homepage displays” is stored in the page_on_front option. When the post being edited matches it, the snippet shows a warning.

The block editor doesn’t show classic admin notices, so for it the snippet adds a small script that creates a native editor notice through core/notices. The classic editor gets a standard admin notice.

Things to watch

  • Nothing is shown if your homepage displays your latest posts, since there’s no homepage page to edit.
  • Page builders with their own editors don’t load these notices.

How to test it

Open the page set as your homepage in the editor. A yellow warning should appear at the top.

This website uses cookies to enhance your browsing experience and ensure the site functions properly. By continuing to use this site, you acknowledge and accept our use of cookies.

Accept All Accept Required Only