WordPress Plugin Updates the Right Way

0
1167

Some weeks ago, I acquired an email approximately WP Photo Sphere, a WordPress plugin I evolved. There became a big problem: updating the plugin broke it on a few installations. After a few investigations, I determined that the hassle got here from the options used by the plugin: these installations hadn’t any default values for the new options I added.

maxresdefault.jpg (1280×720)

Often, converting documents isn’t enough to correctly replace something. For instance, when you manually replace the WordPress files with a new edition, the platform will ask you to hit a button to update the database too.

Assume which you use options on your plugin. As your plugin evolves, you’ll need more options in new variations. Creating new alternatives while a consumer turns on your plugin for the first time is easy; you just have to use the activation hook.

The activation hook can seem a bit difficult. After all, whilst you routinely update a plugin, it’s far deactivated and reactivated, so we should assume this hook to be referred to as. But it’s no longer the case.

Related Contents : 

To be more particular, it becomes, however, WordPress stopped this behavior in model three.1. The improvement team defined this choice, and you may study the whole explanation at the Make WordPress Core blog. The major reason is that it wasn’t called each time because if a user manually updates a plugin, the activation hook may be skipped.

So WordPress doesn’t offer a default manner to call a characteristic proper after a plugin update routinely. That’s why you’ll need to build your very own manner.

How To Handle an Update Process

In this part of the academic, I’ll show you how to call a given characteristic right after updating your plugin robotically. We’ll see within the next element how to properly handle the updates of current alternatives and the advent of new ones (in the identical function).

The Principle of this Method

The international principle of our technique could be that we’ll store the version quantity of our plugin in places: in a constant inside the important file of the plugin and a choice within the database.

The variety inside the database will store the model presently established using the user, at the same time as the quantity within the constant is the current model. If those numbers are extraordinary, then the database alternatives have not been updated since the ultimate plugin update, so we’ll want to do it.

In this example, we name a function that updates all the vital options. This feature also updates the version number stored in the database: that way, we gained call this characteristic greater than vital.

The Constant

Now that we’ve covered what we’ll do, it’s time to code! First, upload a consistent definition in the essential record of your plugin, with your modern version variety as a cost. To prevent any issues, we test if it does now not go out yet.

The Checking Function

We want to jot down a function, a good way to check if the database desires to be up to date. This function will compare the previously described regular with the cost currently saved inside the database. To do this, we’ll make sure that our function is known anywhere, with the movement plugins_loaded induced as soon as all the plugins are loaded.

So why can we call the activation feature? To be clear, we ought to create a brand new function devoted to the update technique. But, if you do this, you’ll see that this new feature could be very much like activation, as updating an alternative may be executed the same manner we create one.

Updating the Version Number within the Database

You can do anything you need within the activation characteristic known above. However, there may be one aspect needed, updating the model variety saved inside the database. That way, we gained call our feature every time a web page is loaded.

Updating any choice may be finished in a theanual manner. We updated the version quantity: you call update_option(), and you’re executed, although it’s the first time WordPress sees the choice.

In reality, in case you use options, it’s regularly for letting your users personalize settings. However, we don’t constantly want to update the alternatives values. Using update_option(), you’ll override the customers’ selections every time you replace the plugin, which isn’t always what we want to do.

Above, we noticed that get_option() returns fake if the choice doesn’t exist. We’ll use this conduct to check if the choice we need to update exists within the database. If this is the case, we don’t do whatever. Otherwise, we create the option.