Friday , 17 November 2017
Home / Programming / WordPress / Plugins / Deactivating A Plugin – WordPress Plugin Tutorial
Deactivating a Plugin
Deactivating a Plugin

Deactivating A Plugin – WordPress Plugin Tutorial

This post is part of the WordPress Plugin tutorial series. To find a link to all tutorials in order, then please click here.

We’ve been learning a lot so far, but our plugins would be useless if the user didn’t have the correct WordPress version. In this lesson, we’re gonna take a look at how to grab the user’s current WordPress version and how we should go about deactivating a plugin.

This function is actually so useful, we’re not going to create a new file. Instead, we’ll be editing our template plugin to use this function so that when we create new plugins in the future, it can be reused easily. So, open up the main plugin file in our template plugin. In our case, it would be index.php.

You should have 3 sections in your template, the set up, includes and hook comment sections. Under our set up comment section, add this bit of code.

Deactivating A Plugin

You may be thinking, why are we putting this in the set up section and not create a separate file for the function and put the register_activation_hook() function under the hooks comment section. When someone activates your plugin, it’s wise to execute this code first before anything else. The above code is something we should check before we do anything else. This also prevents our plugin from crashing the site. Because of that, I believe it should fall under out set up comment section.

Your template file should look something like this.

Let me explain what’s going on. The register_activation_hook() function is a special hook that is triggered when someone activates your plugin. It accepts 2 arguments. The first is the plugin that it should listen for when it’s activated. We use the __FILE__ magic constant because it gives the absolute path to our plugin file. The 2nd parameter is the function we want to run when the hook is triggered.

Our jk_check_WP_ver() function does the process of deactivating a plugin. We use an if statement to check if the current installed WordPress version is lower than the required version to run this plugin. PHP has a function named version_compare() that takes in 2 numeric values and a comparison operator.

We pass in our first numeric value by using the get_bloginfo(  ‘version’ ) function. The get_bloginfo() returns information about our WordPress blog. Inside this function we pass in a string that’ll define exactly what we want. In this case, we want the version. To see a list of all the possible information that can be grabbed from the get_bloginfo() function, click here.

I’m running WordPress 3.5 as of this tutorial. The 2nd value is something we’ll hard code. In this example we set it to 4. The last parameter is the comparison value. Basically, our version_compare would look something like this in human readable code form.

3.5 < 4

This returns true and so the code inside our if statement is initiated. The wp_die() function is a special WordPress function. It’s like the die function except instead of returning a plain white background with black text. It returns a nice, clean, sleek custom message.

Try activating your plugin and see what happens. You should receive an error message saying you must upgrade WordPress. Very simple, yet effective.

Deactivating A Plugin
Deactivating A Plugin

You should know we’re not technically deactivating a plugin this way. We’re actually killing the script before the plugin can be activated in the first place.

Before we end this tutorial, change the 2nd value to 3.1. A fairly good minimal version for our WordPress plugins.

About Jasko Koyn

Check Also

WordPress Meta Boxes

WordPress Meta Boxes – WordPress Plugin Tutorial

WordPress meta boxes are used to display certain information or options in a page. It’s …

Leave a Reply

Your email address will not be published. Required fields are marked *

ULTIMATE WORDPRESS DEVELOPMENT COURSE
Want to learn how to create your own custom WordPress plugins and themes? Check out my course!
Get 93% off my course. You'll also learn how to integrate WooCommerce and BuddyPress into your projects. This is a limited time offer
Yes, I would like to get 93% off your course!
No Thanks!
close-link