Themergency fooplugins.com

Plugin Auto Updates From Github

| 2 Comments

This is day 15 of my WordPress Developer Advent Calendar.

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

So I thought I would take this updater for a spin and create a showcase plugin that uses both the Boilerplate and the Github Updater. You can find it on Github : GitHub Updater Showcase. I used my grunt-init generator for the boilerplate to get a head start. These are the steps to follow to create a boilerplate plugin with the github updater integrated:

  1. Create a new directory in your local WordPress install under wp-content/plugins.
  2. In that new directory run grunt-init wp-boilerplate (to get this setup read my previous post).
  3. Follow the prompts to generate a new plugin using the boilerplate.
  4. 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.
  5. 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
  6.  Back in the admin directory, open the file class-{{PLUGIN-NAME}}-admin.php (this file is only included and executed while in the admin)
  7. 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;
    }
  8. At this point, commit your plugin to a new Github repo.
  9. Copy the Github repo URL and make sure that your plugin header points to the correct URL. In my showcase plugin, it looks like this:
    GitHub Plugin URI: https://github.com/bradvin/github-updater-showcase
  10. 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:

github-updater-showcase

 

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.

2 comments
  Livefyre
  • Get Livefyre
  • FAQ
andyfragen
andyfragen

Brad, you'll also need to include `class-github-api.php`. I've refactored the GitHub Updater plugin to hopefully allow for other repository types. Unfortunately, splitting up the plugin in this manner will result in losing the ability of the updater code to update. It may also cause a fatal PHP error if the class tries to load itself more than once.

themergency
themergency moderator

@andyfragen thanks for the update - I think I need to do another post on this to get it up to speed