The admin_notices WordPress PHP action is used to display custom notices on the admin screen.
Usage
add_action('admin_notices', 'your_custom_function');
function your_custom_function() {
// your custom code here
}
Parameters
- None
More information
See WordPress Developer Resources: admin_notices
Examples
Display a simple notice
This example shows how to display a simple notice on the admin screen.
add_action('admin_notices', 'display_simple_notice');
function display_simple_notice() {
echo '<div class="notice notice-success is-dismissible">';
echo '<p>Your notice has been added successfully!</p>';
echo '</div>';
}
Display an error notice
This example shows how to display an error notice on the admin screen.
add_action('admin_notices', 'display_error_notice');
function display_error_notice() {
echo '<div class="notice notice-error">';
echo '<p>An error occurred. Please try again.</p>';
echo '</div>';
}
Display a notice only on specific admin pages
This example shows how to display a notice only on the “Plugins” admin page.
add_action('admin_notices', 'display_notice_on_plugins_page');
function display_notice_on_plugins_page() {
global $pagenow;
if ($pagenow == 'plugins.php') {
echo '<div class="notice notice-warning is-dismissible">';
echo '<p>Remember to update your plugins regularly.</p>';
echo '</div>';
}
}
Display a notice based on user role
This example shows how to display a notice only to users with the “Editor” role.
add_action('admin_notices', 'display_notice_for_editors');
function display_notice_for_editors() {
if (current_user_can('editor')) {
echo '<div class="notice notice-info is-dismissible">';
echo '<p>Welcome, Editor! Check out the latest posts for review.</p>';
echo '</div>';
}
}
Display a notice with a custom CSS class
This example shows how to display a notice with a custom CSS class.
add_action('admin_notices', 'display_custom_styled_notice');
function display_custom_styled_notice() {
echo '<div class="notice custom-notice is-dismissible">';
echo '<p>This notice has a custom CSS class for unique styling.</p>';
echo '</div>';
}