WordPress Admin Notices API: Dismissible, Persistent, and Per-User Notices

Admin notices that stay dismissed, per user and per screen

The is-dismissible class gives a notice an “×” button, but it only hides the notice in the browser. On the next page load it is back, and users learn to ignore every notice of the plugin. A notice that stays dismissed needs a small AJAX handler that stores the dismissal per user, and a check before printing. While at it, notices should only appear where they make sense, not on every admin screen.

Problem: A plugin shows “Please connect your account” on every admin page, including the block editor, for every user, and the “×” only hides it until the next click. Editors complain, and the notice is ignored by the admin who could actually act on it.

Solution: Show the notice only to users who can fix the problem and only on relevant screens (checked with get_current_screen()), print it with wp_admin_notice(), store a dismissal in user meta through an AJAX request with a nonce, and give dismissals an expiry for reminders that may come back.

Show the notice where it belongs

add_action( 'admin_notices', 'ha_connect_notice' );
function ha_connect_notice(): void {
    if ( ! current_user_can( 'manage_options' ) || get_option( 'ha_api_key' ) ) {
        return;
    }
    $screen = get_current_screen();
    if ( ! $screen || ! in_array( $screen->id, array( 'dashboard', 'plugins', 'settings_page_ha-settings' ), true ) ) {
        return;
    }
    if ( ha_notice_dismissed( 'connect' ) ) {
        return;
    }

    wp_admin_notice(
        sprintf(
            /* translators: %s: settings URL */
            __( 'HA Booking is not connected yet. <a href="%s">Connect your account</a>.', 'textdomain' ),
            esc_url( admin_url( 'options-general.php?page=ha-settings' ) )
        ),
        array(
            'type'               => 'warning',
            'dismissible'        => true,
            'additional_classes' => array( 'ha-notice' ),
            'attributes'         => array( 'data-ha-notice' => 'connect' ),
        )
    );
}

The notice appears only to administrators, only on the dashboard, the plugins screen and the plugin’s own settings page, and never after the problem is solved. wp_admin_notice() (WordPress 6.4+) prints core’s markup and accepts extra classes and data attributes, which the script uses to identify the notice. The block editor does not show admin_notices output anyway; notices there are created in JavaScript with wp.data.dispatch( 'core/notices' ).

Store the dismissal

function ha_notice_dismissed( string $id ): bool {
    $dismissed = (array) get_user_meta( get_current_user_id(), 'ha_dismissed_notices', true );
    $until     = $dismissed[ $id ] ?? 0;
    return $until && ( -1 === $until || $until > time() );
}

add_action( 'wp_ajax_ha_dismiss_notice', function (): void {
    check_ajax_referer( 'ha_dismiss_notice' );
    $id = sanitize_key( wp_unslash( $_POST['notice'] ?? '' ) );
    if ( ! in_array( $id, array( 'connect', 'review' ), true ) ) {
        wp_send_json_error( null, 400 );
    }
    $days      = 'review' === $id ? 30 : -1; // the review reminder comes back after 30 days
    $dismissed = (array) get_user_meta( get_current_user_id(), 'ha_dismissed_notices', true );
    $dismissed[ $id ] = -1 === $days ? -1 : time() + $days * DAY_IN_SECONDS;
    update_user_meta( get_current_user_id(), 'ha_dismissed_notices', $dismissed );
    wp_send_json_success();
} );

Only known notice IDs are accepted, so the meta field cannot be filled with arbitrary keys. A value of -1 means “forever”, a timestamp means “until then”. Per-user storage is right for reminders; for a notice that should disappear for everybody once one admin dismissed it, store it in an option instead.

The script

add_action( 'admin_enqueue_scripts', function (): void {
    wp_enqueue_script( 'ha-notices', plugins_url( 'js/notices.js', __FILE__ ), array(), '1.0.0', true );
    wp_localize_script( 'ha-notices', 'haNotices', array(
        'ajaxUrl' => admin_url( 'admin-ajax.php' ),
        'nonce'   => wp_create_nonce( 'ha_dismiss_notice' ),
    ) );
} );
// js/notices.js
document.addEventListener( 'click', ( event ) => {
    const button = event.target.closest( '.ha-notice .notice-dismiss' );
    if ( ! button ) return;
    const notice = button.closest( '[data-ha-notice]' );
    const body = new URLSearchParams( {
        action: 'ha_dismiss_notice',
        notice: notice.dataset.haNotice,
        _ajax_nonce: window.haNotices.nonce,
    } );
    fetch( window.haNotices.ajaxUrl, { method: 'POST', body, credentials: 'same-origin' } );
} );

WordPress adds the .notice-dismiss button to dismissible notices after the page loads, so the listener is delegated to the document. The core script still hides the notice; this request only remembers it. The AJAX pattern with nonce: nonces in AJAX handlers.

NOTE: Keep notices rare: one notice that requires action, on the screens where the action happens. Upsells and “rate us” notices on every screen are the reason users install plugins that hide all notices. The functions for notices themselves came with WordPress 6.4: WordPress 6.4 for developers.