How to add custom admin notices in WordPress

Admin Notices are used by the WordPress core, plugins, and themes in order to display important information like warnings and notices. You can easily add custom admin notices in WordPress, and this article illustrates how.  

Why are admin notices used in WordPress?

Admin notices are used in WordPress to alert users about warnings, success messages, and errors.  

Admin notices can also be used by individual site owners, as well as plugin authors and theme developers. If you are working for clients who are not familiar with WordPress, then you can choose to display admin notices in order to display helpful information in their WordPress admin area.

Admin notices can also be useful when you are running a multi-author WordPress site. Custom admin notices help guide new authors to familiarize with WordPress..

While there are many benefits to admin notices, it is recommended that they are used carefully. If overused, admin notices can ruin the user experience.

Take a look at how you can add custom admin notices in WordPress.

1: Adding custom notices manually

To add custom notices in WordPress manually, you need to add code to your WordPress site.

Add the following code to the theme’s functions.php file. You can also add this to a site-specific plugin.

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');

This code will display a notice on the settings page. The notice appears with a yellow border. It is accompanied by a button which gives the user an option to close the notice. The settings page appears as shown below:

Upon inspecting the code, you can see the $pagenow variable has been used to detect the current page. Added after that is a condition that checks if the current page meets the page where the notice is to be displayed.  

If everything is ok, then the notice is shown wrapped in a <div> element. This div element uses the CSS classes defined in WordPress admin stylesheet.

You have to use notice class, then you can add notice-error, notice-warning, notice-success or notice-info to add custom admin notices to your WordPress site.

You also have the option to use the in-dismissible class. This adds a button to close the notice.

Depending on the kind of condition and the kind of notice you want to display, you can show the notices that match different scenarios.  

For example, to display a notice only to the users with the user role defined as an author, add the code below:

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');

Upon inspection of the code, you can see that an extra check has been added in the function to detect the user role.

This is how the notice appears on your site:

You can try out different hooks, conditions, and filters to play around with admin notices and choose the ones you require on your site.

2: Adding admin notices with a plugin

This is the simpler of the two methods. It does not require you to add code. However, the flexibility offered by this method is limited in comparison.

1. Install and activate the KJM Admin Notices plugin.

2. Go to Setting –> KJM Admin Notices to configure the plugin settings.

3. Check the option that enables KJM Admin Notices.

4. The second option allows you to add a custom post type. Edit and add your custom admin notices here.

5. Check the box next to “Send Email” option to send emails to registered users when you publish a new notice, if you need this functionality.

6. Check the box next to “Allow Comments” option to enable comments for your notices, if you wish. Users will be able to respond to your notices by adding comments.

7. Click Save.

A new menu item labeled Notices appears in your WordPress admin bar. You can add and edit your custom admin notices here.

To create your first admin notice:

1. Go to Notices –> Add Notice.

2. A screen that resembles the WordPress post edit screen appears.

3. Add a title for your notice, followed by the actual content of the notice. You can also select the notice category from the box in your right-hand side.

4. Add the user roles which will see this notice.

5. Show or hide the title, author, date. You can also choose to show or hide the button to dismiss the notice.

6. Click on the Publish button once you are finished.

Your admin notice now goes live.

The KJM Admin Notices plugin allows you to manage your custom admin notices without having to write any code. You can also choose to delete or unpublish your notices.

You can also use the email feature to alert your users about the notices. This way, your users can read them even when they are not logged in.

You can also choose to use the WP Notification Center plugin. This plugin acts as a Facebook-like notification center in WordPress, making it an accessible location to navigate to notifications.

 

For further questions, or if you need help, please open a support ticket from your HostPapa Dashboard. Click here to learn how to do it.

Related Articles

Get online with our affordable web hosting

Get online with our affordable web hosting

Learn more now
HostPapa Mustache