Admin Notices

This article is part of the WordPress guide. Read the introduction.

Table of Contents

Admin notices are the standard WordPress way of communicating with the user. The system is incredibly simple – you add an action rendering the HTML on the ‘admin_notices’ hook. Let’s say you’re creating an incredibly annoying plugin, which displays a “Thank you for downloading my plugin!” notice on every page. Here’s the code:

PHP
function pgn_annoying_welcome_message() {
    echo '<div class="notice notice-info is-dismissible"><p>Thank you for downloading my plugin!</p></div>';
}
add_action( 'admin_notices', 'pgn_annoying_welcome_message' );

And here’s what you’ll see at the top of every page of the admin panel:

A wrapper div with the ‘notice’ class is required for the message to be styled. The ‘notice-info’ class follows the ‘notice-{type}’ pattern to set the type of the notice. There are 4 types of notices, each of which uses a different accent color:

  • info – blue
  • warning – yellow/orange
  • error – red
  • success – green

Don’t get fooled by the ‘is-dismissible’ class. It’s responsible for adding the “x” on the right to close the notice, but it will not persist when you refresh the page. It is the plugin author’s responsibility to render notices only when appropriate and provide a way to dismiss them. This usually means conditionals in the hooked function and/or custom admin JavaScript (AJAX) communicating with the backend to save the dismissed state in the database.

Table of Contents