Easy WordPress admin notices, including after save_post hook

I ran into a frustrating issue lately trying to display admin notices after a certain event occurred during the save_post hook. Basically, if I tried to hook into admin_notices after a specific event had occurred in the save_post hook, it wouldn’t display because the page refreshes after save_post fires. This is the solution I came up with to easily allow setting and displaying admin notices (including success, warning, error, and info styles), regardless of hook or page refreshes.

Basic Strategy

The inspiration here by an answer someone posted regarding this very issue on the WordPress stackexchange. We can get around the issue of page refreshes by storing all the pending notices in the options table, and then retrieving and displaying them the next time the admin_notices hook fires.

I’ve written a very simple PHP class called Johns_Admin_Notices that has methods you can use for adding info, error, success, and warning messages. You should be able to call these methods at any point during WordPress’s execution, and the notice will be displayed the next time the admin_notices action fires.

An Example

Below is an example of how you would create all the different types of supported messages. You could even call these during the save_post hook without having to worry about the redirect.

add_action('admin_init', function() {
 jp_notices_add_error('These messages');
 jp_notices_add_info('are really');
 jp_notices_add_success('easy to');

And here is how it would look on the next page load:

Gimme the code already

I’ve published the code on GitHub. In the interest of keeping things DRY, I won’t go into too many details here regarding installation or usage. Please do let me know if you have any questions or requests for changes, though!


  1. Barry Brunning on June 17, 2021 at 8:16 pm

    An elegant answer to the widespread frustration I discovered many others were experiencing of the shortcoming of ‘admin_notices’ that I had stumbled upon.

Leave a Comment