Automatic Updates For Private And Commercial Themes

June 2nd, 2011

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 GPLand 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. 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 constructorThe 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.


FindBroken Beta

March 7th, 2011

Here’s something I’ve been working on for the last month or so: FindBroken.com. It’s an automated link checker that periodically scans your site for broken links and alerts you by email if any are found. Basically, it’s like a hosted version of the Broken Link Checker plugin, only simpler and not WordPress-specific.

The site is still in early beta, so some features may a little rough around the edges or just plain missing. Nevertheless, I encourage you to go check it out and let me know what you think. All feedback is welcome.

What already works

  • Add/remove sites.
  • Monitor multiple sites with one account.
  • Automatic daily scans.
  • Email notifications.

What doesn’t quite work

  • The crawler that I use is slow to get going. Sometimes it can take ~30 minutes before any progress is visible.
  • Redirects are currently not followed.
  • There’s a fair amount of false positives. Luckily, most of them are temporary and don’t show up when using the “persistent” filter.
  • The site could use some UX love.

Screenshots


How To Convert Your WordPress Blog To A Static Site

February 2nd, 2011

Recently, I found this old question on WordPress StackExchange:

I am starting a new WordPress blog, and no longer updating an old one. [...] How can I lock the installation of WordPress down so I don’t need to maintain it? I have seen someone suggest making a static version, which sounds like a lot of work.

The top-rated answer is very good, but overly complicated. It involves custom code and .htaccess modification. I think I can offer a simpler solution. Here’s how you can turn your blog into a static site:

  1. Make a backup. Just in case.
  2. Disable comments and any other dynamic features your blog may have, e.g. contact forms.
  3. Install WP-SuperCache and set the cache expiry time to 604800 seconds (= one week).
  4. Fire up Xenu’s Link Sleuth and tell it to crawl your entire site. This will make WP-SuperCache generate static HTML files for all of your pages.
  5. Delete all .php files and drop the WordPress database.

With all PHP scripts deleted, “expired” cache entries will never be removed, so the server will keep serving the same cached pages forever. Congratulations, your site now static :)

Alas, the aforementioned SE thread is rather old, so if I posted my answer there, no-one would probably ever see it.


Page Load Time vs SEO: My Experience

February 1st, 2011

It is hardly news that the average load time is one of the many factors that Google uses to determine how well your site will rank. But how much does it matter, really? If my recent experience is anything to go by, the answer is “very little”.

Over the last [...] Continue Reading…


Lazy-Load Avatars

January 31st, 2011

In my previous post, The Quest For Speed, I mentioned that lazy-loading avatars is a good way to improve your site performance:
The overwhelming majority of visitors never leave a comment. Chances are, most don’t even scroll down to the comments section. So why waste their time and bandwidth by [...] Continue Reading…