Do you want to add admin notices in WordPress? Admin Notices are used by WordPress core, themes, and plugins to display warnings, notices, and important information on the screen for users. In this article, we will show you how you can add admin notices in WordPress.
WordPress uses admin notices to alert users to errors, warnings, and success messages.
Individual site owners, plugin authors, and theme developers can also use admin notices.
If you're working on a website for clients who aren't familiar with WordPress, you can add admin notices to display useful information in your WordPress admin area.
Custom admin notices can also be useful if you run a multi-author WordPress site. You can add prompts to guide new authors and help them find their way around.
However, we recommend using admin notices with care. They can be really annoying and can ruin the WordPress experience for your users.
With that said, let's take a look at how you can add your own custom admin notices in WordPress.
This method requires you to add code to your WordPress site. If you've never added code before, then take a look at our guide on how to paste web snippets into WordPress.
Let us begin.
You must first add this code to your theme's functions.php file or a site-specific plugin.
function general_admin_notice () global $ pagenow; if ($ pagenow == 'options-general.php') echo 'Este aviso aparece en la página de configuración..
'; add_action ('admin_notices', 'general_admin_notice');
This code displays a notice on the settings page with a yellow border and a button to close the notice. This is how it will appear on your site:
If you study the code, you will notice that we have used $ pagenow
Variable to detect the current page.
After that, we add the condition that checks if the current page matches the page where we want to display the notice.
If it does, then we display the notice wrapped in a element. This div element uses CSS classes already defined in the WordPress admin style sheet for different types of notices.
You need to use darse cuenta
class and then you can add aviso-error
, aviso-advertencia
, aviso-éxito
, or aviso-información
.
Optionally, you can use es-descartable
Class that adds a button to close the notice.
In addition to reviewing the current page, you can add all kinds of conditions to display notices that match different scenarios.
For example, you want to display a notice only to users with the author user role.
Here is how you will do that:
function author_admin_notice () global $ pagenow; if ($ pagenow == 'index.php') $ user = wp_get_current_user (); if (in_array ('author', (array) $ user-> roles)) echo 'Haga clic en Publicaciones para comenzar a escribir..
'; add_action ('admin_notices', 'author_admin_notice');
As you can see, we've added an additional check to detect the user's role in our function.
This is how it will appear on your site.
Feel free to practice with different conditions, filters and links to play around with admin notices.
This method is simpler because it doesn't require you to add code. However, it is not as flexible as the custom code method.
The first thing you need to do is install and activate the KJM Admin Notices plugin. For more details, check out our step-by-step guide on how to install a WordPress plugin.
After activation, you need to visit Settings »KJM Admin Notices page to configure plugin settings.
First, you need to check the option to enable KJM admin notices. The second option adds a custom post type where you can add and edit your custom admin notices.
The plugin also allows you to send registered users an email when you post a new notice. You can check the box next to the 'Send Email' option if you want to use this feature.
You can also enable comments for your notices which will allow users to respond to your notices by adding comments. To enable this feature, check the box next to the 'Allow comments' option.
Don't forget to click the Save Changes button to store your settings.
You will now see a new menu item labeled notices in the WordPress admin bar. This is where you can add and edit your custom admin notices.
Let's create your first admin notice.
Visit Notice »Add notice page. You will see a screen very similar to the WordPress post edit screen.
Start by adding a title for your notice, then add the actual notice in the post editor. You can select the notice category in the box on your right hand side.
Next, you need to select the user roles that will see this notice.
Optionally, you can show or hide the title, author and date, and the button to cancel the warning.
Once you're done, click the publish button and your custom admin notice will go live.
KJM admin notifications allow you to manage your custom admin notifications without writing any code. You can delete or unpublish the notices you no longer want to display.
By using the email feature, you can also use it to alert all your users, even if they don't sign up to review the notices.
Having trouble sending emails? Check out our guide on how to fix WordPress not sending email issue.
You may also want to take a look at the WP Notification Center plugin. Add a Facebook-like notification center in WordPress. Users can click the notification icon to see your notifications.
That is all. We hope this article helped you learn how to add custom admin notices in WordPress. You can also check out our guide on how to create a custom user registration form in WordPress.
If you enjoyed this article, please subscribe to our WordPress YouTube Channel video tutorials. You can also find us on Twitter and Facebook.