Display custom message on WordPress admin

In some point of view when we developed a custom WordPress plugin or theme, we have to notify users for any urgent update, error, or any custom message on the WordPress admin section. All this is done by admin_notices hook function.

WordPress Admin Notice

In this article, we are going to play with some codes. We highly recommend taking a backup before doing anything.

In WordPress, there is a particular area to write your own codes and that is through the functions.php file. You can find this file in your theme directory.

function wp231_general_admin_notice() {
    global $pagenow;
	
    if ( $pagenow == 'options-general.php' ) {
		echo '<div class="notice notice-warning is-dismissible">
			<p>My test message on the settings page.</p>
		</div>';
    }
}
add_action('admin_notices', 'wp231_general_admin_notice');

The above codes will generate a custom notice at Settings > General page. As you see that we have used a global variable i.e. $pagenow, which will return the current page URI. In our case, we checked for options-general.php which is the General page URI. This is how you get the admin notice when you visit on the General page.

Admin Notice on General Settings PAge

Let’s discuss a bit more about the notice style. As you see the notice has the yellow shadow on the left side which is because of notice-warning CSS class. There are more CSS classes i.e. notice-warning, notice-error, notice-success, notice-info and you can use it according to your customized notice.

I hope you guys enjoyed this article to create your customized admin notice and if you like this article, then please follow us for more interested and helpful tutorials. You can follow us on Facebook and Twitter.