Adding Custom Admin Notices in WordPress Plugin

Posted on 18th June 2023

As a WordPress developer, you’re likely to come across the need to add custom admin notices to your plugins or themes at some point. Maybe you need to display a warning to users before they take a certain action, or perhaps you want to promote a new feature of your plugin. Whatever the case may be, adding an admin notice is a relatively simple process.

In this article, we’ll discuss what admin notices are and when you might need to use them. We’ll also walk through the process of adding a custom admin notice to a WordPress plugin.

What Are Admin Notices?

Admin notices are messages that are displayed to administrators on the WordPress admin screens. They are used to provide information or warnings to users, or to promote new features or products.

Notices can be either dismissible or non-dismissible. A dismissible notice can be closed by the user, while a non-dismissible notice will be displayed until it is removed by the developer.

When Should You Use Admin Notices?

There are a few different scenarios in which you might want to use an admin notice in your plugin or theme:

  • Display a warning: You can use an admin notice to display a warning to users before they take a certain action. For example, you might want to warn users before they delete a file or make a change to their settings.
  • Display an error message: If there’s an error with your plugin or theme, you can use an admin notice to display an error message to the user. This can be helpful in troubleshooting issues.
  • Promote a new feature: If you’ve added a new feature to your plugin, you can use an admin notice to promote it to users. This is a good way to increase awareness of new features and get more people using them.
  • Upsell a premium product: If you have a premium version of your plugin or theme, you can use an admin notice to upsell users on the benefits of upgrading. This is a common tactic to increase sales of premium products.

Adding a Custom Admin Notice

Now that we’ve discussed what admin notices are and when you might want to use them, let’s walk through the process of adding a custom admin notice to a WordPress plugin.

The first thing you need to do is add the following code to your plugin:

function my_admin_notice() {
?>

<?php
}
add_action( 'admin_notices', 'my_admin_notice' );

This code will display a dismissible notice with the message “Hello, world!” on all admin screens.

If you want to display a non-dismissible notice, you can use the following code:

function my_admin_notice() {
?>

<?php
}
add_action( 'admin_notices', 'my_admin_notice' );

If you want to display a notice only on certain admin screens, you can use the following code:

function my_admin_notice() {
global $pagenow;
if ( $pagenow == 'index.php' ) {
?>

<?php
}
}
add_action( 'admin_notices', 'my_admin_notice' );

This code will display a dismissible notice with the message “Hello, world!” on the dashboard screen.

Conclusion

In this article, we’ve discussed what admin notices are and when you might need to use them. We’ve also walked through the process of adding a custom admin notice to a WordPress plugin.

Adding an admin notice is a relatively simple process, but it can be a helpful way to communicate with users of your plugin or theme.

As a plugin developer, you may want to add custom admin notices to your plugin. These notices can be used to display information to the user, or to display an error message.

Adding a custom admin notice is easy. First, you need to add a hook to the ‘admin_notices’ action:

add_action( ‘admin_notices’, ‘my_admin_notice’ );

Then, you need to define the ‘my_admin_notice’ function. This function will display the admin notice:

function my_admin_notice() {
?>

<?php
}

As we have seen, custom admin notices in WordPress plugins are a great way to keep your users informed about changes, new features, or other important information.

Now that we know how to add custom admin notices to our plugins, let's take a look at how we can style them to match our plugin's branding.

We can start by adding some basic CSS to our plugin:

.my-plugin-notice {
padding: 10px;
border: 1px solid #ccc;
background: #fff;
}

.my-plugin-notice h1 {
margin: 0;
font-size: 16px;
}

.my-plugin-notice p {
margin: 0;
}

This will give our notices a basic style that is consistent with the rest of the WordPress admin.

If we want to go further, we can use the built-in WordPress colors for our notices:

.my-plugin-notice.updated {
border-color: #46b450;
}

.my-plugin-notice.updated h1 {
color: #46b450;
}

.my-plugin-notice.error {
border-color: #dc3232;
}

.my-plugin-notice.error h1 {
color: #dc3232;
}

With this CSS in place, our notices will match the colors of other messages in the WordPress admin.

Of course, you can always style your notices however you like, using your own colors and designs. The sky is the limit!