Automatic Updates For Private And Commercial Plugins

Last updated on June 26, 2015.

Since time immemorial, only plugins hosted in the official WordPress.org plugin directory have supported automatic updates. Now, I’ve written a PHP library that you can use to add automatic update capabilities to any plugin. Public, private and commercial plugins alike – all can now enjoy the benefits of automatic update notifications and one-click upgrades.

The custom update checker integrates closely with the upgrade system already built into WordPress, producing a seamless user experience. Observe :

An upgrade notice for a privately hosted plugin.

An upgrade notice for a privately hosted plugin.

The version information window with placeholder data

The version information window with placeholder data

Download

License

This library is released under the MIT License and is distributed free of charge. If you find it useful, consider making a donation.

Quick-start Guide

This section describes the quickest way to get automatic updates working for your plugin. Here’s what you’ll need to do: create a metadata file for your plugin, host it somewhere publicly accessible, and tell the update checker where to find it.

Lets start with the metadata. Copy the JSON code below into a new file and replace the placeholder values with your plugin’s info.

{
    "name" : "My Cool Plugin",
    "slug" : "my-cool-plugin",
    "download_url" : "https://example.com/plugins/my-cool-plugin.zip",
    "version" : "2.0",
    "author" : "John Smith",
    "sections" : {
        "description" : "Plugin description here. Basic HTML allowed."
    }
}

(This is the minimum amount of data required to make automatic updates work. In most cases, you will probably want to add a couple more fields. See the metadata docs for a full list.)

Most of the fields should be pretty self-explanatory, with one possible exception – the “slug”. WordPress expects all plugins that support automatic updates to have a unique textual identifier called the “slug”. Normally, slugs are assigned by the official plugin directory. For a private/commercial plugin that’s hosted elsewhere you’ll have to make something up. If unsure, just use the plugin’s file name without the “.php” extension (my-cool-plugin/my-cool-plugin.php becomes my-cool-plugin).

Upload the metadata file you just created to your web server. It doesn’t matter where exactly you put the file or how you name it. The important thing is for its URL to be accessible from wherever someone might install your plugin.

Next, copy the “plugin-update-checker” directory from the client library archive to your plugin’s directory. Then fire up your favourite code editor and add the following lines to the top of your plugin file:

require 'plugin-update-checker/plugin-update-checker.php';
$MyUpdateChecker = PucFactory::buildUpdateChecker(
    'https://example.com/path/to/metadata.json',
    __FILE__,
    'your-chosen-slug'
);

If you followed my advice and used the plugin’s file name as the slug, you can omit the third parameter of the PucFactory::buildUpdateChecker() call.

Tip: Sometimes you’ll run into a situation where another active plugin is also using this update checker. As a result, there could be several different versions of the library loaded at the same time. The above code snippet will always give you the latest available version. This can be a problem if your plugin expects an older version and is not API-compatible with the latest version.

To use a specific version of the update checker (e.g. the one included with your plugin), instantiate the PluginUpdateChecker_x_y class directly. Replace x and y with the major and minor version numbers:

//Use version 2.0 of the update checker.
require 'plugin-update-checker/plugin-update-checker.php';
$MyUpdateChecker = new PluginUpdateChecker_2_0 (
    'https://example.com/path/to/metadata.json',
    __FILE__,
    'your-chosen-slug'
);

And that, believe it or not, is it.

The PluginUpdateChecker class will handle the rest. It’ll check the metadata file every 12 hours and, if it discovers that a new version has been released, twiddle the right bits in the undocumented WP API to make it show up as a standard upgrade notification in the “Plugins” tab. Assuming you’ve provided a valid download_url, users will be able to install the update with a single click.

Tip: When creating the ZIP file for an update, put all plugin files inside a directory. The directory name should match the plugin slug. Do not put the files at the root of the ZIP archive – it can cause subtle bugs and errors when someone ties to install the update.

The rest of this post will be devoted to a more in-depth discussion of the update checker class and the metadata format.

The PluginUpdateChecker class

This class is the core of the update checker. It’s also the only part of the updater that you should need to deal with unless you decide to  extend the library yourself.

Class constructor

All configuration settings should be specified by passing them to the PucFactory::buildUpdateChecker() factory method, or directly to the PluginUpdateChecker constructor. Both takes the following parameters:

  • $metadataUrl – The full URL of the plugin’s metadata file.
  • $pluginFile – The path to the plugin’s file. In most cases you can simply use the __FILE__ constant here.
  • $slug – The plugin’s ‘slug’. If not specified, the filename part of $pluginFile (sans “.php”) will be used as the slug.
  • $checkPeriod – How often to check for updates (in hours). Defaults to checking every 12 hours. Set to zero to disable automatic update checks.
  • $optionName – Where to store book-keeping info about updates. Defaults to “external_updates-$slug”.

checkForUpdates()

Manually trigger an update check. This is especially useful when you’ve disabled automatic checks by setting $checkPeriod (above) to zero. This method takes no parameters and returns nothing.

addQueryArgFilter($callback)

Register a callback for filtering query arguments. Whenever the update checker needs to retrieve the metadata file, it will first run each filter callback and attach the query arguments that they return to the metadata URL. This lets you pass arbitrary data to the server hosting the metadata. For example, commercial plugins could use it to implement some kind of authorization scheme where only users that have the right “key” get automatic updates.

The callback function will be passed an associative array of query arguments and should return a modified array. By default, the update checker will add these arguments to the metadata URL:

  • installed_version – set to the currently installed version of the plugin.
  • checking_for_updates – set to 1 if checking for updates, absent otherwise (i.e. when loading data for the “Plugin Information” box).

This method takes one parameter – the callback function.

addHttpRequestArgFilter($callback)

Register a callback for filtering the various options passed to the built-in helper function wp_remote_get that the update checker uses to periodically download plugin metadata. The callback function should take one argument – an associative array of arguments – and return a modified array or arguments. See the WP documentation on wp_remote_get for details about what arguments are available and how they work.

This method takes one parameter – the callback function.

addResultFilter($callback)

Register a callback for filtering plugin info retrieved from the metadata URL.

The callback function should take two arguments. If the metadata was retrieved successfully, the first argument passed will be an instance of PluginInfo (see the source for a description of this class). Otherwise, it will be NULL. The second argument will be the corresponding return value of wp_remote_get (see WP docs for details). The callback function should return a new or modified instance of PluginInfo or NULL.

This method takes one parameter – the callback function.

Metadata format

The automatic update system uses a JSON-based file format to describe plugins.  Essentially, the entire file is one big JSON-encoded object (AKA hash-table or associative array). Each field – or array key – represents a piece of information about the latest version of the plugin. The full description of all available fields is here.

For the sake of simplicity, both general metadata and update-related information are stored in the same file. If this is undesirable, you can replace the plain JSON file with a script that checks for the presence of the the “checking_for_updates” query parameter and emits just the update-related fields if its set to “1”.

Notes

Your plugin must be active for updates to work. The update checker is just another piece of PHP code loaded and run by your plugin, and it won’t be run if the plugin is inactive.

One consequence of this that may not be immediately obvious is that on a multisite installation updates will only show up if the plugin is active on the main site. This is because update notifications usually appear in the network admin, and only plugins active on the main site are loaded in that case. The main site of a WordPress network is the one that was created first and has the path “/” in the Sites -> All Sites list.

Related posts :

496 Responses to “Automatic Updates For Private And Commercial Plugins”

  1. Jānis Elsts says:

    Try changing line 10 in plugin-update-checker.php to this:

    if ( !class_exists('PluginUpdateChecker_2_0', false) ):
    
  2. William says:

    Thank you for a quick reply: unfortunately… the same error.
    Fatal error: Class ‘PluginUpdateChecker_2_0’ not found in /Applications/MAMP/www/support/wp-content/plugins/c960d-ledger/plugin-updates/plugin-update-checker.php on line 1137

  3. William says:

    Think I have it, well, the errors are gone: Changed all … new StdClass to new \StdClass. Now lets see if I can get a reply!

  4. William says:

    Thanks for the assistance! Works like a charm, pushing to download folder with redirect to zip and count incriminator to capture downloads count. Creating a homepage URL plugin vote and admin side online plugin.zip uploader with comment section to make pushing out updates faster & easier, plus holds history of older versions.

  5. Jānis Elsts says:

    Duly noted. I will patch all of the class_exists() calls in the public release soon.

    As for StdClass, there really should be no need to add the root namespace to that. Strange. Perhaps the class-loader is doing something unusual.

  6. William says:

    If you are doing updates, around line 868 in the plugin-update-checker needs a few field updates. Here is the current object returned (hope is doesn’t turn out too ugly ):
    [1] => stdClass Object
    (
    [name] => bbPress
    [slug] => bbpress
    [version] => 2.5.7
    [author] => The bbPress Community
    [author_profile] => //profiles.wordpress.org/johnjamesjacoby
    [contributors] => Array
    (
    [matt] => //profiles.wordpress.org/matt
    [johnjamesjacoby] => //profiles.wordpress.org/johnjamesjacoby
    [jmdodd] => //profiles.wordpress.org/jmdodd
    [netweb] => //profiles.wordpress.org/netweb
    )

    [requires] => 3.6
    [tested] => 4.2.2
    [rating] => 78
    [num_ratings] => 142
    [ratings] => Array
    (
    [5] => 81
    [4] => 15
    [3] => 16
    [2] => 7
    [1] => 23
    )

    [active_installs] => 200000
    [last_updated] => 2015-04-28 5:49pm GMT
    [homepage] => http://bbpress.org
    [short_description] => bbPress is forum software, made the WordPress way.
    [icons] => Array
    (
    [svg] => //ps.w.org/bbpress/assets/icon.svg?rev=978290
    [2x] => //ps.w.org/bbpress/assets/icon-256×256.png?rev=978290
    [1x] => //ps.w.org/bbpress/assets/icon-128×128.png?rev=978290
    )

    )

  7. Jānis Elsts says:

    Do you mean the “active_installs” and “icons” fields? I’ll add “active_installs” sometimes soon, but I’m not sure if “icons” are applicable to plugin updates. Does WP actually display them anywhere?

  8. William says:

    Just for convenience, I listed the entire response object. I did not implement the icons array or the short_description.

    The other fields were built from an index.php file that returns a json object; that way I could programmatically update the active_installs, num_ratings, ratings fields.

    I created my own wp-plugin-update tool many years back and was having trouble updating it to the ‘new’ way. Thank you very much for your expertise there.

    Lastly, I noticed that if you click update version and then the Install button inside the modal pop up, the install update fails. This was also true with my old way of updating.

  9. Jānis Elsts says:

    Lastly, I noticed that if you click update version and then the Install button inside the modal pop up, the install update fails. This was also true with my old way of updating.

    I can’t seem to reproduce that issue on my test site. The “Install Update Now” button works fine and installs the update.

    There’s probably something else going on, like another plugin triggering a notice and messing up the AJAX response. See also this related issue:
    https://github.com/YahnisElsts/wp-update-server/issues/28

  10. Gotta say I was doubting this whole thing for a couple of hours but finally got it working. What tripped me up was the zip file. You have to make a zip file with the plugin folder inside, not the plugin files.

    Example or zip contents : my plugin.zip > my plugin folder > my plugin files

    Also make sure the version number in your new version is higher than the old version and that it matches the version in your json file.

    Awesome stuff once you get your head around it.

    Thanks to you guys.

  11. Sterling says:

    Your amazing! Thanks for the tutorial it was exactly what I needed, donation on its way!

  12. Jitender says:

    Hi,

    Thanks for the awesome Tutorials.

    I have used the same code you have written above and it’s working almost fine but there is little issue coming in my case.

    once i clicked on update button it showing me plugin has been updated successfully. but it’s version is not updated and also again on page reload the update notice is coming again.

    Please help me if you can.

    Thanks in advance.
    Jitender

  13. Fatih says:

    Hi, is there a way to force an update? without always going to the asmin panel and press the update button?
    Thank you for the great script its verry usefull for me spacially if i write my own plugins for our webistes.

  14. Jānis Elsts says:

    Do you mean something like: as a developer, force sites to install an update without user participation? No, there’s no built-in way to do that, and there probably won’t be. Of course, you could implement something like that yourself.

  15. Paul says:

    Hello,

    I have tried what you suggested in this tutorial but still no luck. Even I clicked on the “Check for updates” link of the plugin, it just displayed the message as “This plugin is up to date.”.

    I already made sure that the version number in the JSON file hosted on my server is higher than the installed one.

    Also, when I called “checkForUpdates()” function, it returned “Fatal error: Call to undefined function checkForUpdates()”.

    So I am not sure what I did wrong. Here is the code I put in my main plugin file:

    require ‘plugin-update-checker/plugin-update-checker.php’;
    $myUpdateChecker = new PluginUpdateChecker_2_2(
    ‘http://url.to/file.json’,
    __FILE__,
    ‘plugin-slug’,
    1
    );

    Note that I am currently working on localhost.

    Could you please advise?

    Thank you for your time!

  16. Paul says:

    OK I found it! I used single quote instead of double quote for the string in my JSON file and it didn’t pass the validation. After fixing this, the auto update is working properly.

    Thanks for your amazing work! 🙂

  17. Tjerk says:

    Wonderful stuff. Very easy to implement and I had it up and running within a couple of minutes. I made a small donation for your efforts.

  18. Jānis Elsts says:

    Thank you for the donation!

  19. Thomas McGee says:

    Hi there,

    All of the sudden, I’ve noticed that a plugin I’ve built using this library says “This plugin is up to date.” after checking for updates—even though I have checked and double checked that there is a new version specified in the json file.

    Is there something I need to adjust on my end?

    Thanks!

  20. Jānis Elsts says:

    My guess would be that it fails to download and parse the JSON file.

    There are several ways to test that. One is to enable WP_DEBUG and try checking for updates again. Then see if there are any new messages in the PHP error log. Alternatively, install Debug Bar, click “Debug” in the admin bar, then open the “PUC (your-plugin-slug)” section and click the “Request Info” button. The update checker will download the JSON file, parse it, and display some debugging information.

Leave a Reply