PersistedWordPressAdminNotices:Part

Persisted WordPress Admin Notices: Part 2

In part one of this series, we learned how to implement a basic admin notice that appears at the top of every WordPress admin page. In this tutorial, we’ll start to build out a plugin to contain all our custom admin notice code.

We’ll begin by implementing standard admin notices and use them as a base for more flexible and advanced examples.

Setting Up Our Plugin

First, though, let’s set up a new plugin from scratch that we’ll be using for all our admin notices so we’re ready to start entering code.

I’ll assume here that you already have a local WP development site set up. If not then refer back to the links in part one of this tutorial series.

Create a new plugin folder called admin_notices inside /wp-content/plugins/, and then create an admin_notices.php file which will be the main plugin file.

Open up admin_notices.php in your favorite editor and add the basic plugin structure:

We added a basic plugin header so WordPress recognises our plugin. This is followed by a class that will contain methods to display our admin notices.

I named the class Gwyer_Admin_Notices to try and make it as unique as possible. This way, it’s much less likely to conflict with an existing class name.

Let’s start by displaying a basic admin notice, and then add to it to make it more useful. To create an admin notice, add the admin_notices hook to the init() function:

The hook includes a test_notice callback function which will be used to output the admin notice markup.

Add the following class method to Gwyer_Admin_Notices to display the actual admin notice. For the messages, we’ll be using classic movie quotes from the last 100 years of movies.

Powered by WPeMatico

Leave a Comment

Scroll to Top