How to update WordPress plugins automatically?

In a recent sprint, we made a remarkable revamp for the UI architecture of Prevent Direct Access (PDA) plugin and its extensions: Membership Integration and File Access Restriction. We faced a problem that when the customers update the new version 3.1.0 of PDA, they also have to update the next version 1.1.6 of Membership Integration and 1.1.4.5 of File Access Restriction. We have tried to find a solution with the aim to make it easier for the customers. The idea is that once the users update PDA, its extension will be updated to the new versions automatically.

In this post, we will describe the steps of how we deal with the problem. Before discovering the instruction, let’s take a quick look at how to use the WordPress hook auto_update_plugin.

Using auto_update_plugin hook to enable auto-updates

The auto_update_plugin hook is introduced since WordPress version 3.7. We can use the code below to enable auto-updates for specific plugins.

We add our 2 extensions to WordPress Automatic background update process. As a result, they won’t update immediately, and we have to wait until the next event schedule. It’s clear that we need a better solution instead of having to wait.

Using the upgrade method of Plugin_Upgrader class

After diving deep into the WordPress codebase, we explored that WordPress used Plugin_Upgrader class to handle the plugin updates.

$upgrader = new Plugin_Upgrader();
$upgrader->upgrade( $your_plugin_slug );

However, while upgrading the plugins, it will show the update information to the admin dashboard. An own class inherited from WP_Upgrader_Skin class is required to hide the feedback message.

Then, we passed our own skin to Plugin_Upgrader constructor.

Last but not least, don’t forget to activate the plugins after updating them successfully. Here is the completed source code:

We manage to resolve the auto-update plugin problem with these simple steps. Hope you find the mentioned method useful. Please subscribe and leave your thoughts below if you have any comments or cooler ideas.

Leave a Reply

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