do_action( 'admin_notices' )

Prints admin screen notices.


Source Source

File: wp-admin/admin-header.php

View on Trac


Top ↑

Changelog Changelog

Changelog
Version Description
3.1.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by Patrick

    Sample Usage

    function sample_admin_notice__success() {
        ?>
        <div class="notice notice-success is-dismissible">
            <p><?php _e( 'Done!', 'sample-text-domain' ); ?></p>
        </div>
        <?php
    }
    add_action( 'admin_notices', 'sample_admin_notice__success' );

    The class notice-success will display the message with a white background and a green left border.

    function sample_admin_notice__error() {
    	$class = 'notice notice-error';
    	$message = __( 'Irks! An error has occurred.', 'sample-text-domain' );
    
    	printf( '<div class="%1$s"><p>%2$s</p></div>', esc_attr( $class ), esc_html( $message ) ); 
    }
    add_action( 'admin_notices', 'sample_admin_notice__error' );

    The class notice-error will display the message with a white background and a red left border.

    Use notice-warning for a yellow/orange, and notice-info for a blue left border.

    The class name is-dismissible will automatically trigger a closing icon to be added to your message via JavaScript. Its behavior, however, applies only on the current screen. It will not prevent a message from re-appearing once the page re-loads, or another page is loaded.

    [copied/pasted from https://codex.wordpress.org/Plugin_API/Action_Reference/admin_notices%5D

  2. Skip to note 2 content
    Contributed by Benny

    The Theme Review Team (TRT) released a package you can use in your theme or plugin to create admin notices:
    https://github.com/WPTRT/admin-notices

    Its primary purpose is for providing a standardized method of creating admin notices in a consistent manner using the default WordPress styles.

    Notices created using this method are automatically dismissible.

  3. Skip to note 3 content
    Contributed by jakeparis

    In order to display a notice, echo a div with the class notice and one of the following classes:

    * notice-error – will display the message with a red left border.
    * notice-warning– will display the message with a yellow/orange left border.
    * notice-success – will display the message with a green left border.
    * notice-info – will display the message with a blue left border.
    * optionally use is-dismissible to add a closing icon to your message via JavaScript. Its behavior, however, applies only on the current screen. It will not prevent a message from re-appearing once the page re-loads, or another page is loaded.

    Don’t use update-nag as a class name! It is not suitable for regular admin notices, as it will apply different layout styling to the message. Additionally it will trigger the message to be moved above the page title, thus semantically prioritizing it above other notices which is not likely to be appropriate in a plugin or theme context.

    The inner content of the div is the message, and it’s a good idea to wrap the message in a paragraph tag for the correct amount of padding in the output.

  4. Skip to note 4 content
    Contributed by Munir Mahmud
    //Display admin notices 
    function my_test_plugin_admin_notice()
    {
    	//get the current screen
    	$screen = get_current_screen();
    
    	//return if not plugin settings page 
    	//To get the exact your screen ID just do ver_dump($screen)
    	if ( $screen->id !== 'toplevel_page_YOUR_PLUGIN_PAGE_SLUG') return;
    		
    	//Checks if settings updated 
    	if ( isset( $_GET['settings-updated'] ) ) {
    		//if settings updated successfully 
    		if ( 'true' === $_GET['settings-updated'] ) : ?>
    
    			<div class="notice notice-success is-dismissible">
    				<p><?php _e('Congratulations! You did a very good job.', 'textdomain') ?></p>
    			</div>
    
    		<?php else : ?>
    
    			<div class="notice notice-warning is-dismissible">
    				<p><?php _e('Sorry, I can not go through this.', 'textdomain') ?></p>
    			</div>
    			
    		<?php endif;
    	}
    }
    add_action( 'admin_notices', 'my_test_plugin_admin_notice' );
    

    The class notice-success will display the message with a white background and a green left border and the class notice-error will display the message with a white background and a red left border.

    if you want to display the errors you can also use notice-warning for a yellow/orange for a blue left border.

You must log in before being able to contribute a note or feedback.