Simple way to mirror a WordPress theme or plugin folder from a Github project

Written by:
Posted in: ,
Published: September 5, 2013

We have a server where we like to showcase stable versions of our WordPress plugins and themes, which are developed locally and hosted on Github. Instead of committing to Github and then uploading the files to your server, you can use the steps below to easily and automatically pull changes from Github to your server whenever you make a commit.

SSH into your server and set up your Github project. If you’re setting up a theme, for example, cd into your /wp-content/themes/ directory and execute git clone /local-folder-name/ to copy your Github project onto your server for the first time.

Create a new file in your theme folder, call it github.php, and paste in the following code:

[code php]<?php `git reset –hard HEAD`; ?>;
<?php `git pull`; ?>;
<?php `find . -type d -print0 | xargs -0 chmod 0755`; ?>
<?php `find . -type f -print0 | xargs -0 chmod 0644`; ?>[/code]

What this code does is executes shell commands via PHP by placing them in backticks. The first line resets the current project so that all changes will be overwritten from the master copy at Github. The second line pulls all new changes from your Github project. The third line sets file permissions to 755 for folders (the default for mosts hosts). The second line sets file permissions to 644 for all files.

Next, log into Github and go into your project settings for that project. Under Service Hooks, click WebHook URLs and add the url to the github.php file on your server. For example:

Now, whenever you update your project on Github, changes will be pulled immediately over to your staging site.