Do you want to add notifications to the admin panel in WordPress? Admin notifications are used by the WordPress core, themes, and plugins to display warnings, notifications, and important information for users on the screen. In this article, we will show you how to add notifications to the WordPress admin panel.
Why And When Should I Use Admin Messages In WordPress?
WordPress uses notifications in the admin panel to inform users of errors, warnings and messages about the successful completion of a task.
Also, these notifications are used by site owners, plugin authors, and theme developers.
If you are working on a client site and the client is not familiar with WordPress, then using notifications you can display useful information throughout the site.
However, we do not recommend abusing them, because notifications can become annoying.
Let’s see how you can add your own admin. Notifications in WordPress.
Method 1: Add Your Own Notifications To WordPress Manually
This method will require you to add code to your site.
To get started, you will need to add the following code to the functions.php file of your theme or to the plugin for the WordPress site :
1 |
function general_admin_notice(){ |
2 |
global $pagenow ; |
3 |
if ( $pagenow == 'options-general.php' ) { |
4 |
echo '<div class = "notice notice-warning is-dismissible" > |
5 |
<p>This notification will appear on the settings page.</p> |
6 |
</div>'; |
7 |
} |
8 |
} |
9 |
add_action( 'admin_notices' , 'general_admin_notice' ); |
This code will display a notification on the settings page with a yellow stroke and a button to close the notification. Here is how it will look on your site:
If you study the code, you will see that we used the $ pagenow variable to determine the current page.
After that, we added a condition that checks whether the current page matches the one where we need to display a notification.
If so, then we display a notification, framed in a <div> element . This div uses CSS classes already defined in the WordPress style sheet for various types of notifications.
You need to use the notice class , and then add notice-error , notice-warning , notice-success or notice-info .
If necessary, you can use the is-dismissible class , which adds a button to close the notification.
In addition to checking the current page, you can add any conditions for displaying notifications.
For example, you need to display notifications only to users with the role of the author on the site.
Here’s how to do it:
1 |
function author_admin_notice(){ |
2 |
global $pagenow ; |
3 |
if ( $pagenow == 'index.php' ) { |
4 |
$user = wp_get_current_user(); |
5 |
if ( in_array( 'author' , ( array ) $user ->roles ) ) { |
6 |
echo '<div class = "notice notice-info is-dismissible" > |
7 |
<p>Press on<a href= "edit.php" >Records</a> in order to start an article.</p> |
8 |
</div>'; |
9 |
} |
10 |
} |
11 |
} |
12 |
add_action( 'admin_notices' , 'author_admin_notice' ); |
As you can see, we added an additional check to the function to determine the user’s role.
This is how it will look on the site.
Experiment with different conditions, filters and hooks for your needs.
Method 2: Add Admin.Informations Using The Plugin
This method is simpler and does not require adding code. However, it is not as flexible as the previous one.
The first thing you need to install and activate the plugin KJM Admin Notices .
After activation, go to the Settings page »KJM Admin Notices to configure the plugin.
First mark the activation option KJM Admin Notices. The second option adds an arbitrary record type, where you can add and edit your own admin notifications.
The plugin will also allow you to send emails to registered users when you post a new notification. You need to check the box next to ‘Send Email’ if you decide to use this feature.
You can also include comments for your notifications, which will allow users to respond to notifications by posting comments. To enable this feature, check the box next to ‘Allow Comments’.
Do not forget to save the changes made.
Let’s create the first admin. Notification.
Go to the page Notices. Add Notice . You will see a page like to create a new entry.
You should start by specifying the name for the notification, then add the notification itself in the record editor. You can select a category for notifications in the box on the right.
Next, we specify the user role for which the notification will be displayed.
Additionally, you can hide or show the title, author and date, as well as a button to hide the notification.
After finishing, click on the publish button and the notification will be available to everyone.
KJM Admin Notices allows you to manage your notifications in the admin panel without code edits. They can be deleted or unpublished if necessary.
Using the email function, you can also use the plugin to notify your users, even if they have not logged into the admin area.