Pass WordPress Admin Notices Across Page Redirects

ADVERTISEMENT:
persistent-messages

I’ve been writing a lot of WordPress plugins recently, and one task keeps popping up, that doesn’t seem to have a definitive answer in the WordPress core.  I’ll need to show a message (confirmation, success, error, notice, warning, info, etc) to a user, after redirecting them through one of the scripts used to save changes, such as admin_post.php (for example, using the save_post action with a custom metabox).

Because I’m a strong proponent of DRY programming (Don’t Repeat Yourself), I wanted a universal tool to solve the problem, once and for all.  I’ve created a small class that I call WP_Persistent_Notices.  It’s a singleton, and is pluggable, so there should be no issues with simply including it in a theme or plugin, as is, and not worrying about another theme or plugin also including it.

It provides a simple filter interface for adding an admin notice, as well as a helper function to simplify the call, or allow it to more easily be used with variables.  The helper can be called as follows:

<?php
add_persistent_notice( array(
    'type' => 'error',
    'message' => "Houston, we have a problem!"
) );
?>

By default, notices are rendered at the top of the page, by the ‘admin_notices’ action, but notices can alternatively be defined with an optional ‘location’ attribute, that will allow you to render them anywhere, with

<?php render_persistent_notices( $location ); ?>

Here’s the Gist I created for the class:

FAQ

Why do you use a session variable to store the notices, instead of {insert method here}?

It seemed like the best compromise at the time.  POST/GET parameters cause issues with refreshing, COOKIE can only be set before rendering content, so it limits the user’s ability to add a notice at any point, and transients are slower and create potential concurrency issues when two users are working with the same account.

What if my server config can’t use sessions, or I need some special configuration?

There are three filters ( ‘wp_persistent_notices_replace_initialization’, ‘wp_persistent_notices_replace_save_notices’, and ‘wp_persistent_notices_replace_retrieve_notices’ ) that can be used to transparently replace the SESSION functionality with any other functionality of your choice, such as transients or memcache storage.

What’s with sorting the notices by type?

It seems to be the most logical way to waterfall the importance of multiple notices, but if you don’t like it, there is a filter to disable, a filter to replace, and a set of filters to change the weights of the various notice types (including custom types).

Why isn’t this class part of WordPress core?

I don’t know, that’s not my department.  Maybe Automattic will see this post and feel similarly, or maybe you can plus one my response to the ticket at: https://core.trac.wordpress.org/ticket/11515#comment:27

Your class is bad, and you should feel bad!

Well, that’s not really a question, but ok… why not contact me about it, or just fork the gist? ..or best yet, do both!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>