How to display admin notices on switching theme?

admin notices

As you work in the WordPress Admin Panel, you’ll certainly face “admin notices” that let you know about errors, updated settings, required actions, and so on.

In this article you will understand to display the admin notice after switching theme.

We’ve described the process in some steps:

1.  At first, you will need to add the activation hook given below.

/* Register Hook Activation. */
add_action( 'admin_init', ‘my_theme_update_admin_notice' );
function my_theme_update_admin_notice() {
if ( isset( $_GET[‘example_hide_notice'] ) && $_GET['example_hide_notice'] ) {
update_option( ‘my_theme_show_notice', 1 );
}
}

2. Then you will need to add the notice text which is to show for the theme on activation.

add_action( 'admin_notices', ‘my_theme_render_admin_notice' );
/* Admin Notice on Activation. */
function my_theme_render_admin_notice() {

/* Check option, if not available then display notice */
if( ! get_option( ‘my_theme_show_notice' ) ) {
$url = add_query_arg( 'example_hide_notice', '1' );
?>
/** Code goes here **/
 }
}

3. At last, you will need to delete the option to remove the notice when switching on another theme.

/* Delete admin notice on switch theme. */
add_action('switch_theme', ‘my_theme_remove_option');
function my_theme_remove_option () {
delete_option( ‘my_theme_show_notice', 1 );
}

Hope this article will help you. 🙂

Leave a Comment