Persisted WordPress Admin Notices: Part 4

So far in this series, we’ve covered two separate ways to dismiss persistent WordPress admin notices. We’ll build on that in this fourth and final part of the tutorial series by looking at two more specific methods to permanently dismiss your admin notices. We’ll round things off by showing how to create your own custom admin notice types and add decorations such as icons.

Sticky Admin Notice

We already know how to display an admin notice that can be dismissed. All we have to do is add the is-dismissible CSS class to the containing div element. However, this is only temporary and will only dismiss the notice for the current page. As soon as the page reloads, it reappears again.

To make it permanently dismissible involves more code than we’ve seen so far, but it isn’t too difficult to implement. Let’s take a look at what’s involved, starting with an overview.

We’ll use a custom option to store the display status of our admin notice. On plugin activation, this option will be created/updated and set to true. The admin notice will then only display if the option is currently true.

The key to this method is using Ajax to allow us to set the option to false when the dismiss button is clicked. Once successfully set to false, the conditional code that checks the option status will fail, and the admin notice will no longer be displayed.

Let’s begin by adding the admin notice itself, which will be a plain notice to begin. In Gwyer_Dismissible_Admin_Notices::init(), add a new add_action call:

Then add the dismiss_admin_notice() callback function to the same class:

Powered by WPeMatico

Leave a Comment

Scroll to Top