Webprogramo > Blog > Wordpress > WordPress admin notices after a page refresh (across redirects)

WordPress admin notices after a page refresh (across redirects)

Let's display WordPress admin notices across page redirects.

Alejandro Lopez

8 agosto, 2017

Webprogramo > Blog > Wordpress > WordPress admin notices after a page refresh (across redirects)
SHARE! and the author will get a Yummy Cookie :
Share this...
Share on Facebook
Facebook
Tweet about this on Twitter
Twitter

Not often we need to create admin notices after a page refresh on WordPress, but, well, it happens ¯\_(ツ)_/¯.

I ran into this issue when creating a plugin and programming inside the «save_post_{$post->post_type}» and «save_post» actions of WordPress. At that moment, I needed to display an error after the execution of my function, but, it did not work because WordPress does a redirect and the admin_notice I created was removed.

So I crafted this solution to display admin notices after a page refresh on WordPress, and I wanted to share it with you. =).

WordPress displays admin notices on top of an admin page, with a special markup

<div class="{class}">
{message}
</div>

The {class} can be «notice-error,» «notice-success,» «notice-warning» or «notice-info, « and if you want to remove the notice with a JavaScript function, you have to add an extra class named «is-dismissible.» So, to create admin notices after a page refresh on WordPress (I will call it Flash Notices, yes, good old times programming with Codeigniter), we have to add this code on our functions.php (or plugin file).

/**
 * Add a flash notice to {prefix}options table until a full page refresh is done
 *
 * @param string $notice our notice message
 * @param string $type This can be "info", "warning", "error" or "success", "warning" as default
 * @param boolean $dismissible set this to TRUE to add is-dismissible functionality to your notice
 * @return void
 */

function add_flash_notice( $notice = "", $type = "warning", $dismissible = true ) {
    // Here we return the notices saved on our option, if there are not notices, then an empty array is returned
    $notices = get_option( "my_flash_notices", array() );

    $dismissible_text = ( $dismissible ) ? "is-dismissible" : "";

    // We add our new notice.
    array_push( $notices, array( 
            "notice" => $notice, 
            "type" => $type, 
            "dismissible" => $dismissible_text
        ) );

    // Then we update the option with our notices array
    update_option("my_flash_notices", $notices );
}

/**
 * Function executed when the 'admin_notices' action is called, here we check if there are notices on
 * our database and display them, after that, we remove the option to prevent notices being displayed forever.
 * @return void
 */

function display_flash_notices() {
    $notices = get_option( "my_flash_notices", array() );
    
    // Iterate through our notices to be displayed and print them.
    foreach ( $notices as $notice ) {
        printf('<div class="notice notice-%1$s %2$s"><p>%3$s</p></div>',
            $notice['type'],
            $notice['dismissible'],
            $notice['notice']
        );
    }

    // Now we reset our options to prevent notices being displayed forever.
    if( ! empty( $notices ) ) {
        delete_option( "my_flash_notices", array() );
    }
}

// We add our display_flash_notices function to the admin_notices
add_action( 'admin_notices', 'display_flash_notices', 12 );

First, We create a function to store our admin notices, and then, add a function to «admin_notices» action to verify if there are stored notices, if there are, display them and remove our option to prevent notices be displayed forever.

Admin notices examples

Finally, to create a notice you just add them with «add_flash_notice». E.g.,

add_flash_notice( __("My notice message, this is a warning and is dismissible"), "warning", true );
add_flash_notice( __("My notice message, this is an info, but, it is not dismissible"), "info", false );
add_flash_notice( __("My notice message, this is an error, but, it is not dismissible"), "error", false );

I know this code can be improved, but, it is a good starting about admin notices after a page refresh on WordPress (Across Redirections). I hope it is useful =).