Plugin Guide: Version Checking, Activation & Admin Notices

Extension Development

Last Updated: Time to Read: 4 minutes

This tutorial assumes you have completed the other sections of the plugin guide and have a working extension. Now that your extension is almost complete, you will need to add some final functions to ensure Layers exists and is at a version that is compatible with your plugin.

In the public function area of the class file, the first function we need to create is layers_version_check()

Line 2:  Set <a href="https://codex.wordpress.org/Function_Reference/wp_get_theme" target="_blank">wp_get_theme()</a> to the layers template name layerswp . For this to work, Layers must be installed with an unmodified template name in the style.css or the active child theme must have the template  set to layerswp .

Line 4: Here we use the version_compare  PHP function to compare the value of the Version in the installed Layers theme header with the minimum version we defined in our main file constant and <  sets out condition if the istalled Layers version is lower than our defined minimum then:

Line 5: Set our instance of update_required  (explained below) to the version defined in our constant OR if the installed Layers version detected on line 4 is equal to or greater than our defined version:

Line 6: Set our instance of update_required to FALSE so no notice is shown.

Now you will need to setup some notices to display in the admin if Layers is not installed, a Layers theme is not active, or Layers needs to be updated. Create a new public function using a unique name:

Inside this function you can be relatively free-form since we will be hooking the content of this function into admin_notices to display in the WordPress admin if our condition is met. The following demonstrates how to setup two notices that will work with our version check and link the user to the right Layers resources .

Line 1: Grab the current site’s ID for multisite support via the global $blog_id varible. See get_current_blog_id

Line 2: Setup a  variable to represent the installed themes. See <a href="https://codex.wordpress.org/Function_Reference/wp_get_themes" target="_blank">wp_get_themes()</a>

Line 3: Double-checking to ensure the currently active theme is NOT Layers. Here we will look to see if Layers is already reporting that it needs an update OR if it is not active, whether the theme even exists via get_template().

Line 4: We have decided to use the WP core CSS classes is-dismissible  and error  for our notification display. This creates a clean notice with a red flag that the user can close. You can use the notice  class instead of error for a green flag, or add a custom class here and style it further via any admin CSS you have enqueued.

Line 5-6: This is the content of the notice. For a Layers Required message, please copy/paste this example as it contains the correct URL for downloading. On Line 6, we add a quick check of the list of themes installed and look for layerswp

isset( $themes['layerswp'] )

If its there, we use <a href="https://codex.wordpress.org/Function_Reference/admin_url" target="_blank">admin_url(</a><span style="background-color: #d2eeca;">)  to generate a link inside the notice that goes to the Appearance → Themes page and lands on Layers so the user can quickly Activate it. If it is NOT there, the link url gets set to our download page instead.

? admin_url( 'themes.php?s=layerswp' ) : "http://www.layerswp.com" )

Note how our slug constant is used in this example to represent the text domain value for the _e()  function that allows localization of our notice text.

Line 8:  Here we will check to see if the result of this instance of our update_required  check is not FALSE and if so, show the following notice

Line 9: Again we use the WP notice CSS classses in our wrapper.

Line 10: Setup a _e()  function to hold your text string. $this->update_required  will output the required version number and the remainder will ensure the user can download the Layers updater from our website.

In your plugin class __construct()  setup your instance of layers_version_check , then hook your notices custom function, which we called activate_demo_extension_admin_notice into the WP Core hook admin_notices :

Next, we will just setup a quick condition case Layers is already updated or Layers is active, the notices are not triggered.

With the error  class:

admin-error

With the notice  class:

admin-notice