Themergency

Plugin Auto Updates From Github

I have been uploading a lot of code lately to Github in both my personal and FooPlugins accounts. I love Github. It makes collaboration so easy, and I am starting to move all my WordPress.org plugins over to Github so others can fix bugs and add features. One project on Github which you have to check out is Tom McFarlin’s WordPress Plugin Boilerplate. If you read the docs, you will see that it supports the Github Updater.

Github Updater Showcase Plugin

  1. Create a new directory in your local WordPress install under wp-content/plugins.
  2. Follow the prompts to generate a new plugin using the boilerplate.
  3. Download a zip of the github-updater code and extract it somewhere. This project handles updates for themes and plugins but we only care about the plugin updater code.
  4. We only want our plugin to do update checks while in the admin, so we need to copy 2 files from the classes directory in the zip, to our generated directory wp-content/plugins/{{PLUGIN-NAME}}/admin/includes. The files to copy are:
    class-github-updater.php
    class-plugin-updater.php
  5.  Back in the admin directory, open the file class-{{PLUGIN-NAME}}-admin.php (this file is only included and executed while in the admin)
  6. Clear the code that is in the constructor function __construct, and add the following code:
    private function __construct() {
        require_once 'includes/class-github-updater.php';
        require_once 'includes/class-plugin-updater.php';
        new GitHub_Plugin_Updater;
    }
  7. At this point, commit your plugin to a new Github repo.
  8. Bump the version number in your plugin’s header and commit the changes again.

Test An Auto Update

The plugin updater is a very slick piece of code, and you do not need to play around with git tags and releases at all. It looks at the version number in the plugin header and compares that to your locally installed plugin. So to test this, simply edit the local plugin header and change your version number to something lower than what is in the repo. Then goto your WordPress Updates page in the backend and click “Check Again” (sometimes I have needed to click this button up to 3 times). You should then see something similar to this:

Showcase Plugin

If you are lazy and do not want to follow the above steps, then simply download my github-updater-showcase plugin and install it. Then change the version number in the header to 0.0.1 to test it updates to version 0.0.2.