Automatic Updates For Private And Commercial Themes

Update 2017-06-20: This library has been deprecated. Please use PUC instead. It’s more current and it supports both themes and plugins.


This is a PHP library that lets you add automatic update notifications and single-click updates to any WordPress theme. It’s purpose is to be easy to integrate for developers and to provide a familiar experience to theme users. From the users’ point of view, update notifications generated by this library will look and function just like those displayed by WP itself.

Dashboard screenshot

An update notification for a theme not hosted on wordpress.org

Download

License

This library is licensed under the GPL and is distributed free of charge. If you find it useful, consider making a donation. Commercial licensing (e.g. for projects that can’t use an open-source license) is available upon request.

Quick-Start Guide

There are two things you will need to do:

  1. Create a publicly accessible “metadata file” that describes the latest version of your theme.
  2. Add the update checker to your theme and tell it where to find that file.

First, the metadata file. Open your favourite text editor and copy the following JSON code into a new file:

{
  "version" : "2.0",
  "details_url" : "http://example.com/example-theme/details.html",
  "download_url" : "http://examle.com/example-theme/example-theme.zip"
}

Replace the placeholder values with your own data. As you can probably guess, version is the version number of your theme. details_url specifies the page that the user will see if they click the “View version 1.2.3 details” link in an update notification. Set this field to your “What’s New In Version 1.2.3” page or the theme homepage (tip: if you notice that your page looks strange when viewed from the WP dashboard, see this comment).

Finally, download_url is the URL where the latest version of the theme can be downloaded. This field is optional. If you leave it out, the user will still get an update notification when a new version comes out, but there will be no “update automatically” link. They’ll have to download and install the update manually.

Upload the metadata file to your website. You can use any directory and file name you like; just remember that the file URL should be accessible from wherever someone might install your theme.

Next, lets add the update checker library to you theme. Copy the “theme-updates” directory from the client library to your theme. Then add the following to your functions.php:

//Initialize the update checker.
require 'theme-updates/theme-update-checker.php';
$example_update_checker = new ThemeUpdateChecker(
    'example-theme',
    'http://example.com/example-theme/info.json'
);

Again, replace the placeholders with your own settings. The first argument should be the name of your theme’s directory. For example, if your theme lives in /wp-content/themes/my-theme/, use “my-theme” here. The second argument should be the URL of the metadata file you just created.

Congratulations, your theme now supports automatic updates 🙂 The update checker will automatically query the metadata file every 12 hours, checking to see if a new version is available. If it finds one, it will display a standard theme update notification on the Dashboard. Your users will be able to install the new version with a single click.

The ThemeUpdateChecker class

Class constructor
The library is configured by passing a number of arguments to the ThemeUpdateChecker constructor. They are, in order :

  • $theme –  The theme directory name, sometimes called the “slug”.
  • $metadataUrl – The URL of the theme metadata file.
  • $enableAutomaticChecking – Enable/disable automatic update checking. If set to FALSE, you’ll need to explicitly call the checkForUpdates method to, err, check for updates. Defaults to TRUE.

checkForUpdates()
Manually trigger an update check. This is useful if you want to do update checks on your own schedule. checkForUpdates has no parameters and does not return anything. If you want to actually retrieve the latest update, use requestUpdate instead.

requestUpdate()
Retrieve update information from the configured metadata URL. Returns either an instance of ThemeUpdate, or NULL if there is no newer version available or if there’s an error.

deleteStoredData()
The update checker stores various update-related bookkeeping data in a DB option. Call this method to delete that data. This is can be useful is your theme provides some kind of “uninstall” feature.

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 themes could use it to implement some kind of authorization scheme where only paying users 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 append the following query arguments to the URL:

  • installed_version – the currently installed version of the theme.

This method takes one parameter – the callback function.

addHttpRequestArgFilter($callback)
Register a callback for filtering arguments passed to wp_remote_get. The callback function should take one argument – an associative array of arguments – and return a modified array or arguments. See the WP documentation 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 theme info retrieved from the metadata URL. The callback function should take two arguments. If a theme update was retrieved successfully, the first argument will be an instance of ThemeUpdate. 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 an instance of ThemeUpdate, or NULL. This method takes one parameter – the callback function.

Related posts :

306 Responses to “Automatic Updates For Private And Commercial Themes”

  1. […] Link Posted on November 14, 2012 by admin […]

  2. Sleek says:

    Works like a charm. Thanks

  3. David Gwyer says:

    This is a great script for enabling auto theme updates.

    One thing though, it is vital that we can validate a user to make sure they have correct permission to access theme updates. Obviously the validation code needs to be on our server, and we can add some form fields to the theme options page to collect a username and password.

    Any ideas on the best place in your auto update script to perform this validation?

  4. Jānis Elsts says:

    You could use the addQueryArgFilter() method to append your license key (or what have you) to the all update requests and then validate the key on your server. An example:

    function addKeyToUpdateRequests($query) {
        $query['key'] = get_option('my_theme_license_key');
        return $query;
    }
    $updater->addQueryArgFilter('addKeyToUpdateRequests');
    
  5. David Gwyer says:

    I’m not sure I follow exactly. How does the ‘all update requests’ get received by my server, so I can add my own code? i.e. how do I trigger a callback to run my validation code and halt or continue the update process?

    Sorry, not delved into this aspect of WordPress before.

  6. Jānis Elsts says:

    My suggestion was only about the part of the update process that happens on the user’s/client’s site. The code example illustrates how you can attach additional data to update requests that your theme will send to your server.

    How you handle those requests on your server is entirely up to you; this library doesn’t handle that. Presumably, instead of using a static metadata file, you could write a little script that examines each request to ensure that it contains a valid license key (for example), and then decides whether to output the update information or not. Then you could point the update checker to that script, and only users who have entered a valid key would get updates.

  7. […] 同学们都知道,提交到WordPress官方的主题可以自动检测更新,还支持在线一键更新。那对于WordPress主题开发者来说,如果主题没有提交到WordPress官方,是否也可以让主题拥有该功能呢?答案是肯定的,最近倡萌在w-shadow.com上看到了相关文章,并且已经在 Htwo_pro 主题测试成功,一起分享下。 […]

  8. […] Shadow’s libraries for automatic updates for private and commercial themes and updates for private and commercial plugins work perfectly together with IWP thus giving me the […]

  9. Barbara says:

    Is there anything special I have to do to use this on a child theme? I’ve set this up, and used your plugin to check for updates immediately – but it isn’t recognizing the update.

  10. Jānis Elsts says:

    No, it works the same way with child themes. In fact, the example theme is a child theme – you can try it out yourself and see how it’s implemented. The download link is in the post.

    There are a several reasons why an update might not show up:

    • The metadata file is not valid. Try running it through JSONLint.
    • The metadata URL is incorrect. Try accessing it in your browser to check if it works.
    • The theme directory name is wrong. Make sure you’ve replaced “example-theme” with the actual name.
  11. Barbara says:

    Thanks for the quick reply, but still stumping me. Checked that the file is valid. Can access the metadata file from my browser. Changed the theme directory name.

    Using the plugin to check for updates – nothing. It’s got to be something simple, yes?

  12. Barbara says:

    I just installed the example theme, but it doesn’t show any updates available either.

  13. sam says:

    I just installed the example theme, but it doesn’t show any updates available either.

    I have also implemented it in my theme and does not show the updates link in the theme.

    Jānis Elsts Please advise.

  14. Jānis Elsts says:

    Try going to Dashboard -> Updates to trigger a check.

  15. Jonny says:

    Confirmed working!

    Great work.
    Thanks

  16. Barbara says:

    I have a very newbie question, as this is my first theme that I’m adding the auto updates to – and I haven’t been able to find this answer in my research. When I have a theme update, do I zip the entire theme up as the download, or am I only supposed to include files that have been changed?

  17. Jānis Elsts says:

    You need to zip the entire theme. While WordPress does support partial/”delta” updates for the WP core, themes and plugins must include all files in the update.

  18. Barbara says:

    Thanks so much!

  19. Barbara says:

    I have another issue I can’t figure up. I’m trying to include an html file with the changelog for the theme. I have the file being read, but it’s doing something weird.

    When I click on the link, it’s bringing up the page but it’s like it’s super-zoomed in. I can’t see the top of the file, nor the x to close the overlay. If I zoom out my browser about 3 times I can see everything, but when it first loads it’s really zoomed in.

    Any idea how I can correct this?

  20. Jānis Elsts says:

    Hmm, I’ve never heard of a problem like that. Could you maybe post a screenshot?

Leave a Reply