Dev Tip #1: How to Add a Custom Admin Notice in WordPress

Sometimes your plugin or theme needs to show a message in the WordPress admin area — maybe a success message after saving settings, or a warning if something is missing.

Here’s how I usually do it:
Keep it simple, clean, and native.

The Snippet

add_action( 'admin_notices', function() {
    if ( ! current_user_can( 'manage_options' ) ) {
        return;
    }

    echo '<div class="notice notice-success is-dismissible">';
    echo '<p>Your custom admin notice goes here.</p>';
    echo '</div>';
});

How It Works

  • Hooks into admin_notices, which is used by WordPress to display messages at the top of admin screens.
  • notice-success gives it a green success style. You can also use:
    • notice-error (red)
    • notice-warning (yellow)
    • notice-info (blue)
  • is-dismissible makes it closable.
  • Always wrap with a capability check like manage_options to avoid showing notices to users who don’t need to see them.

Bonus: Show it only on specific pages

You can limit where the notice appears like this:

if ( get_current_screen()->id === 'settings_page_my-plugin' ) {
    // show notice
}

I use this pattern in almost every plugin I write.
Clean admin messages go a long way in making your tools feel more polished and professional.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *