Auto-Update Theme Not Hosted on WordPress Repository

How do you make a WordPress Theme part of the auto update check. I know you can plug in to the plugin auto updater, to add/remove plugins from the auto updater, but how do you do this with themes?

I tried digging through the Twenty Ten theme, but there is no code anywhere which defines how it auto updates, or registers it for auto update. Yet, it auto updates with WordPress.

Read More

Any help would be greatly appreciated.

EDIT: Should have specified, my theme is not in the WordPress repository. It will be distributed separately.

Related posts

Leave a Reply

4 comments

  1. Because your theme does not reside on the WordPress repository, an easy methodology is to incorporate file access in your theme. A quick way to do this:

    1. Incorporate version control within a master file in your theme. Create a “version.php” file that has a PHP variable like version = 1.1
    2. Create a directory where your theme files will be hosted on your own site. Create a “version.txt” file in that directory that only contains the latest version number (i.e.: 1.2) and no other text or numbers. The URL might look like domain.com/repository/version.txt.
    3. Design your theme to open the contents of domain.com/repository/version.txt and use PHP to compare the numbers of each. If there is a newer version, then download the latest version of the theme as a ZIP.

      $version = floatval(file_get_contents('domain.com/repository/version.txt'));
      // note use only 1 decimal to keep it simple and prevent floatval() from failing

      if($version > $localversion) {
      copy("domain.com/repository/version".$version.".zip","theme/tmp/version_temp.zip");
      $zip = new ZipArchive;
      $res = $zip->open("theme/tmp/version_temp.zip");
      if ($res === TRUE) {
      $zip->extractTo("theme");
      $zip->close();
      echo 'ok';
      } else {
      echo 'failed';
      }
      }

    You’ll need to take that code, refine it, and account for file permissions and what works best for performance.

  2. The update API is split in three: core, plugins and themes. All are hosted on wp.org, and the mere existence of your plugin/theme in the WP repository makes it auto-updated without a line of code beyond the standard plugin/theme headers and readme.txt files.

  3. wordpress.org has an API listening for plugin/theme update requests. The client is the local WordPress installation that sends those requests to WordPress.org, and waits for a reply. The local WordPress installation uses wordpress.org as a default, but the remote API can be any custom URL, such as example.com.

    If your local WordPress installation is on example.com it will need a custom plugin that is built to act as an API that listens for update requests using HTTP(s) from your plugin/theme installed elsewhere, or even on the same server.

    For a plugin/theme to send the API request to a server, such as example.com, rather than wordpress.org, you would need to build client software, such as a client class, to send the API request to example.com, and when the client receives the HTTP(s) response it would hook into one of two filters:

    pre_set_site_transient_update_plugins
    
    pre_set_site_transient_update_themes
    

    One filter hook is for plugins and one filter hook is for themes. Those are not the only hooks available in WordPress.

    To summarize, a client library needs to be built for the plugin for theme to send HTTP(s) requests to an API located on a server, such as at example.com. A plugin must also be built and installed on the server, such as on example.com, for an API to listen for client HTTP(s) requests.

    What is done with those HTTP(s) requests on the client and server can be customized however you’d like, but it takes time to develop a solution. There are free and commercial solutions available that may meet your immediate needs, or you can use one of those solutions as a start to create your own custom solution.

    Here is the request and response flow:

    client ->(HTTP(s)) request)-> server(API)
    server(API) ->(HTTP(s)) response)-> client
    

    Here are two solutions as an example:

    (Free) wp-update-server from YahnisElsts

    (Commercial) WooCommerce API Manager

    There are other solutions and tutorials out there currently that a Google search can help locate.

    Keep in mind, any solution you develop should have security at the forefront, so as not to expose your server to a hack, especially since you are exposing an API on your server. This is one reason I included a commercial solution as one of many solutions available.