upgradewordpressoneclick

How Do I Code One-Click WordPress Plugin Updates?


My client wants me to make a plugin and sell it through a membership subscription system. They asked how they can roll out updates for this plugin. Typically I have had the plugin intercept the dashboard viewing after a login into wp-admin. It then checks if a newer version of the plugin is on the server. If so, it notifies the user so that they can download it again with a click. The problem is that they must unzip the files, copy them over, and resave the plugin settings.

Is there a way to code a one-click update for the plugin when it's due for an update? That way, it eliminates the other steps where they download the plugin, unzip, copy over the old files, and rerun an admin panel save for the plugin.


Solution

  • There are a few steps that you'd probably want to take, most of which can be easily borrowed from how wordpress handles it's own internal updates.

    Don't run it every time the admin page is loaded, write your own version of _maybe_update_plugins() and have it check on a timed interval, probably only once per day or so.

    the WordPress WP_Upgrader class in 'wp-admin/includes/class-wp-upgrader.php' (I believe you will need to include that in your plugin) do what you want. Take a look at wp-admin/update.php and make sure that you emulate the security precautions here very closely as you could introduce huge security holes if not done carefully.

    Once you've sorted out your notifications and made sure that the process is secure, it's as easy as passing the url for the new version of your plugin to the code below as "$download_link".

    $upgrader = new WP_Upgrader;
    $upgrader->run(array(
      'package' => '', //this should be the name of your plugin
      'destination' => '', //this should be defined to the directory you want to install the plugin to
      'clear_destination' => false, //set this if you want to remove the old version first
      'clear_working' => true, //change this if you want to leave a copy of the zip file
      'is_multi' => false, //only change this if you're calling the function multiple times
    ));
    

    If that doesn't do the trick (I've not the time or motivation to test it), dig around a bit more in the WP_Upgrader class and borrow their functions for downloading and extracting files.