Do you want to add admin notices in WordPress? Admin notifications are used to display alerts, notifications, and important information on the screen to the user. In this article, I will show you how to do it most effectively. Let’s find out together.

Contents
Why and When to Use Custom Admin Messages in WordPress?
WordPress uses admin notifications to alert users to errors, warnings, and success messages.

Individual website owners, plugin authors, and theme developers can also use admin notices.
If you are working on a website for a client who is not familiar with WordPress, then you can add a custom admin message to display useful information on their WordPress admin area.
Custom admin messages can also be useful if you run a multi-author WordPress site. You can add notifications to guide new authors and help them find their way.
However, I recommend using admin notices carefully. They can be quite annoying and ruin the WordPress experience for users.
Let’s see how you can add your own custom admin messages.
Manually Add Custom Admin Messages in WordPress
First you need to add this code to your theme or child theme’s functions.php file:
function general_admin_notice() {
global $pagenow;
if ( $pagenow == 'options-general.php' ) {
echo '<div class="notice notice-warning is-dismissible">
<p>This notice appears on the settings page.</p>
</div>';
}
}
add_action( 'admin_notices', 'general_admin_notice' );
<div>
<span>1</span><span>2</span><span>3</span><span>4</span><span>5</span><span>6</span><span>7</span><span>8</span><span>9</span>
</div>
This code shows a notification on the settings page with a yellow border and a button to close the notification. Here’s how it will appear on your site:

If you study the code, you will notice that the variable $pagenow is used to detect the current page.
Then I added a condition to check if the current page meets the page where I want the message to be displayed.
If yes, then I display the message wrapped in a <div> element. This div element uses predefined CSS classes for different message types.
You can change these classes to suit your needs, namely: notice-error , notice-warning , notice-success , or notice-info
You can add the class is-dismissible , using this class will appear an additional button to close the message.
In addition to checking the current page, you can add all kinds of conditions to display messages suitable for different situations.
Example of adding a custom message in WordPress
For example, you want to show a message only to users with the author role.
Here’s how you would do it:
function author_admin_notice() {
global $pagenow;
if ( $pagenow == 'index.php' ) {
$user = wp_get_current_user();
if ( in_array( 'author', (array) $user->roles ) ) {
echo '<div class="notice notice-info is-dismissible">
<p>Click on <a href="edit.php">Posts</a> to start writing.</p>
</div>';
}
}
}
add_action( 'admin_notices', 'author_admin_notice' );
<div>
<span>1</span><span>2</span><span>3</span><span>4</span><span>5</span><span>6</span><span>7</span><span>8</span><span>9</span><span>10</span><span>11</span><span>12</span>
</div>
I added an extra check to detect the user’s role.
This is how it will appear on your website.

Feel free to practice with different conditions, filters, and hooks to customize the admin message to your needs.
Epilogue
With this approach, you can easily add admin notices in WordPress.
If you find it interesting, you can follow the WordPress basics section to know more new knowledge.
Follow fanpage to receive the latest posts: Group

