PersistedWordPressAdminNotices:Part

Persisted WordPress Admin Notices: Part 1

WordPress admin notices provide a convenient way to display messages to users in the admin area, e.g. after a post has been updated or a plugin has been activated. They’re also used by many themes and plugins to display notifications about anything from new features or security warnings to details about ongoing promotions or upgrade prompts.

WordPress core provides four different admin notices which can be used contextually to alert a user to a specific type of notice. This is achieved by displaying a unique accent color for each type of admin notice.

Admin notices are usually displayed at the top of every admin page to stand out from the main page content and be clearly noticeable. They are elegantly designed so as to not be too visually distracting.

WordPress core admin notice

WordPress also reuses admin notices in other locations throughout the admin interface, such as when a theme or plugin update is available. Core usage of admin notices isn’t limited to just being displayed at the top of the admin screen.

WordPress admin notices in alternative location

Displaying an admin notice in a custom plugin or theme is relatively easy, requiring just a few lines of code, as we’ll discover shortly. However, WordPress doesn’t provide a way by default to dismiss a persisted admin notice.

Even though you can add a dismiss button to any admin notice, this doesn’t prevent it from reappearing when the page is reloaded. Also, admin notices appear on every admin page, which is far from ideal.

If you want to have granular control over exactly when and where admin notices are displayed and to be able to dismiss them effectively, you’ll need to add custom code to modify the default behavior.

What We’ll Be Covering

We’ll start at the beginning and explore the basics of implementing admin notices via a custom plugin, including displaying them only on specific pages in the WordPress admin.

Admin notices appear on every page by default, which isn’t always what you want. For instance, you may only want to display a notification on a plugin options page. So our next port of call will be to conditionally display admin notices depending on the current admin screen.

Building on this, we’ll introduce ways to further manage admin notices by controlling when they appear too. Rather than appearing as soon as the page loads, they will only appear if certain triggering conditions are met. This could be useful, for example, if you wanted to display an admin notice on a plugin options page, but only after settings had been saved.

As mentioned above, there’s no easy way to dismiss persisted admin notices between page loads. So the remainder of the tutorial series will focus mainly on various methods you can employ to dismiss admin notices so they won’t reappear unexpectedly.

Finally, for a bit of fun, we’ll see how you can create your own custom admin notice types and add additional decoration such as icons to your admin notices.

By the end of this tutorial series, you’ll be able to display any type of admin notice anywhere within the WordPress admin. Plus, you’ll be able to decide whether to show them on page load or via a custom action, and you’ll also be able to dismiss them in a variety of different ways, depending on your needs.

Want to Follow Along?

You’ll get most out of this tutorial series if you follow along as we build out each admin notice example. The code is presented in a step-by-step approach to allow you to build a working plugin on your own as we progress through the tutorial. However, if you don’t wish to type in all the code yourself, the finished plugin will be available for download in part four.

It’s assumed that you have at least a rudimentary working knowledge of WordPress plugin development, including how hooks work. If not, then I’d recommend reading up on these topics via the official WordPress documentation before continuing:

  • Introduction to Plugin Development
  • Writing a Plugin
  • WordPress Hooks

In order to test the plugin code for each example, you’ll need a working WordPress site. The easiest way to do this is to install WordPress locally so you have easy access to edit files. 

There are many choices for developing locally with WordPress, including:

  • Local by Flywheel
  • DesktopServer
  • MAMP (and MAMP PRO)
  • Vagrant

If you’re new to WordPress development then Local or DesktopServer are probably easiest to set up. Local is free to use (they have a premium version in the works), and DesktopServer has a limited (free) version plus a premium version available.

Also, some previous experience of PHP and JavaScript is recommended, as is some experience of implementing Ajax requests. However, everything will be explained along the way, so in-depth knowledge isn’t required.

A Closer Look at Admin Notices

Let’s take a look at the most basic implementation of an admin notice and the code required to render a success type notice.

Powered by WPeMatico

Leave a Comment

Scroll to Top