How we update our WordPress theme
With three of us working on the core WordPress theme which makes up the 12412 site, we wanted to be sure we could all easily make updates to it – but without conflicting with each other and in a way that made the theme open and available for anyone to look at.
The second part of this requirement was simple: we work with the theme in a Git repository, and are using Github both as the “origin” repo and a place to host and give access to the theme files for anyone who is interested in seeing them. The theme itself is based upon Paul’s Slim Starkers, which is itself a further cut-down fork of Elliot Jay Stocks’ Starkers and provides a nice, clean starting point for your own layouts. Between us we have varying experience of Git as a source control system, so working in this way is also helping to build up the skills of the whole team.
The first part, allowing us all to push theme updates to the 12412 site, required a little more thought.
One great feature of Github is its facility for post-commit hooks, which allows you to trigger a HTTP post to an external service whenever you push to the repository. Typically this is used a signal to the service that you want it to fetch the new commit from the repo and do something with it – in our case it would simply be to deploy the files. There are a couple of documented ways of doing this tailored to WordPress, requiring Git to be installed on your web server, and ultimately we’ve evaluated this method as being one to try in the future.
Back in the now, we still needed a simple but effective way to get our updates live. Of course you could manually copy files up after making a change in your local repo, but there is a risk of conflict here and over-writing other work if you’re not careful about pushing and fetching code from the origin. Perhaps there was something out there a bit neater.
If you’re familiar with WordPress’s theming system, you’ll know that they can be versioned through meta data in the style.css file. This is what triggers updates in your site’s admin panel when new versions of publicly hosted themes are released but it doesn’t work if the theme is hosted on Github. However a bit of searching turned up the Theme Updater plugin, which promises to do exactly that.
Now in our style.css file there is a new piece of metadata, Github Theme URI, which should be set to the location of the Github repo.
The Version of the theme is then incremented for each version we want to update with, using SemVer principles, and that commit in the repo is tagged with the same version number.
Finally, in the admin interface, the plugin polls for updates from Github in exactly the same way as a regular theme and – when a new tagged commit is seen – prompts for the update to be applied.
In this way we get to hook into WordPress’s own theme updater which means all the niceties such as switching to maintenance mode are taken care of.
So far it seems to work brilliantly. We may later on look at a post-commit hook to automate our updates, but for the time being the plugin is doing exactly what we need it to.
There is a slight caveat in that it only works with public Github repos, but I’m sure it wouldn’t be impossible to put some authentication in the plugin to access private ones – should you need it.
