Persisted WordPress Admin Notices: Part 3

Welcome back to our series on persisted WordPress admin notices. We’re now in a good position to be able to extend our admin notice functionality and control when they appear on the page.

After that, we’ll move on to persistent admin notices and see how you can make sure they’re dismissed in such a way that they don’t reappear when admin pages are reloaded. 

This is particularly important as custom admin notices implemented in plugins and themes are only really useful when you can permanently dismiss them. It’s not enough to simply add the is-dismissible CSS class.

Controlling When Admin Notices Appear

Up until now, all admin notices have been displayed as soon as the page loads. There may be times when this is inconvenient and you want the admin notice to be displayed after a certain event has been triggered instead.

How about if we wanted a custom admin notice to be displayed on the plugin options page we created earlier, but only after the Save Changes button was clicked?

We already know from part two how to restrict an admin notice to the plugin options page. Let’s find out how we can trigger it to appear after plugin options have been saved.

Begin by commenting out the add_action function call for plugin_admin_notice. Then, add a fourth add_action call in init as follows:

Whenever plugin options are saved, a settings-updated value of true is added to the super global $_GET array variable. We can use this to determine if we need to display our admin notice.

Add a new conditional_plugin_admin_notice method to Gwyer_Admin_Notices that outputs the value of $_GET to the screen.

Powered by WPeMatico

Leave a Comment

Scroll to Top