Add custom admin notices in WordPress with PHP

Custom admin notices: simple, conditional and dismissible

Admin notices are the coloured boxes at the top of admin screens: “Settings saved”, “Please update”, “Configure the plugin”. Plugins print them on the admin_notices hook. Used well, they tell the administrator exactly what to do; used badly, they are the reason people hate plugins. The difference is in showing a notice only to the right user, on the right screen, and letting them get rid of it.

Problem: A plugin needs to remind administrators to enter an API key, show a success message after an action, and let users dismiss a notice permanently.

Solution: Print notices on admin_notices with the core classes (notice notice-success|info|warning|error, is-dismissible), check the capability and the current screen, and store a permanent dismissal in user meta through a small AJAX handler.

A simple notice

<?php
add_action( 'admin_notices', function () {
    if ( ! current_user_can( 'manage_options' ) || get_option( 'ha_api_key' ) ) {
        return;
    }

    printf(
        '<div class="notice notice-warning"><p>%s <a href="%s">%s</a></p></div>',
        esc_html__( 'HA Plugin needs an API key to work.', 'textdomain' ),
        esc_url( admin_url( 'options-general.php?page=ha-settings' ) ),
        esc_html__( 'Enter it now', 'textdomain' )
    );
} );

The notice disappears by itself once the key is set: the condition is the dismissal.

Only on some screens

<?php
add_action( 'admin_notices', function () {
    $screen = get_current_screen();

    if ( ! $screen || 'edit-product' !== $screen->id ) {
        return; // only on the product list
    }

    echo '<div class="notice notice-info"><p>' . esc_html__( 'Tip: use bulk edit to change prices of many products at once.', 'textdomain' ) . '</p></div>';
} );

After an action

After a form handler redirects back with a parameter (for example ?ha_saved=1), show the result once:

<?php
add_action( 'admin_notices', function () {
    if ( isset( $_GET['ha_saved'] ) ) {
        echo '<div class="notice notice-success is-dismissible"><p>' . esc_html__( 'The settings were saved.', 'textdomain' ) . '</p></div>';
    }
} );

is-dismissible adds the × button. It hides the notice in the browser, but the notice comes back on the next page load, which is fine for one-time messages.

Dismissed for good

<?php
add_action( 'admin_notices', function () {
    if ( ! current_user_can( 'manage_options' ) || get_user_meta( get_current_user_id(), 'ha_welcome_dismissed', true ) ) {
        return;
    }

    printf(
        '<div class="notice notice-info is-dismissible ha-welcome" data-nonce="%s"><p>%s</p></div>',
        esc_attr( wp_create_nonce( 'ha_dismiss_welcome' ) ),
        esc_html__( 'Thanks for installing HA Plugin! Start with the settings page.', 'textdomain' )
    );
} );

add_action( 'wp_ajax_ha_dismiss_welcome', function () {
    check_ajax_referer( 'ha_dismiss_welcome', 'nonce' );
    update_user_meta( get_current_user_id(), 'ha_welcome_dismissed', 1 );
    wp_send_json_success();
} );

add_action( 'admin_footer', function () {
    ?>
    <script>
    jQuery( document ).on( 'click', '.ha-welcome .notice-dismiss', function () {
        jQuery.post( ajaxurl, { action: 'ha_dismiss_welcome', nonce: jQuery( this ).closest( '.ha-welcome' ).data( 'nonce' ) } );
    } );
    </script>
    <?php
} );

The click on core’s × button (added by is-dismissible) sends one AJAX request, and the notice never appears again for that user. The delegated handler is needed because WordPress adds the button with JavaScript after the page has loaded.

NOTE: Show notices only where they matter and only to users who can act on them. A notice on every admin screen, for every user, that cannot be dismissed, trains people to ignore all notices, including the important ones from WordPress itself.