Distributing Your Plugins in GitHub with Automatic Updates

WordPress has made it convenient for plugins to be deployed to the public. Just put your plugin in the WordPress plugin repository and it can be easily found by people from inside their dashboard.

Probably one of the best features of the repository is that it makes it easy to update plugins from inside the dashboard. Update notifications are displayed inside the dashboard, and performing an update is as easy as clicking the Update Now button.

But how about if you wanted to host the plugin on your own? The convenient update feature won't be available if you host your plugin outside the WordPress plugin repo. Nor would your users be notified of an available update.

This article will teach you that with a little creative coding, you can host your own WordPress plugins in GitHub while still retaining the automatic update feature.

Why Host Your Plugin in GitHub?

There may be a number of reasons why you would want to host your plugin outside the WordPress plugin repository. One of the main restrictions the repository gives is that you are required to license your plugin as GPLv2. I won't dwell on the details of licensing, but in a nutshell it means that anyone can share your work. So if you want to sell your plugin, then hosting it in a private GitHub repository is now an option you can consider.

However, because of the way WordPress was built, hosting your plugin in GitHub would disable automatic updates for our plugin. To understand why this is the case, we have to understand how WordPress handles plugin updates.

You can learn more about WordPress plugin repository by reading their Detailed Plugin Guidelines.

How WordPress Updates Plugins

First, let's investigate  how WordPress performs plugin updates in order to get a better understanding of why self-hosted plugins can't have automatic updates

WordPress periodically checks the WordPress repository for updates your installed plugins. It receives a bunch of information on each plugin such as its latest version and the plugin's package URL. This is what we usually see in the plugin admin page when we are notified of an update:

Plugin Admin Update Now

When we click the View version x.x details link, WordPress performs another lookup in the plugin repo. This time it gets more detailed information about the plugin like it's description, changelog, what WordPress version its tested in, and plenty more. These information are shown to us in a lightbox:

Plugin Lightbox

Lastly, when the Update Now link is clicked, the updated plugin package is downloaded and installed.

So why doesn't automatic updates work for self-hosted plugins? It's because WordPress tries to find it in the WordPress plugin repository and can't find it there!

The Game Plan

Our plan is to enable automatic updates with our GitHub-hosted plugin.

Here's a list of what we need to do to make it work:

  • We need a way to deploy plugin updates in GitHub.
  • We should show plugin version update notifications,
  • It would be nice to display plugin details when the View version x.x details link is clicked.
  • We also want to successfully install the plugin update when the update now link is clicked,

How to Execute the Plan

We will be using some WordPress filters in order for us to implement our game plan. These are:

  • pre_set_site_transient_update_plugins. This filter is called when WordPress tries to check for plugin updates.
  • plugins_api. This one is used when WordPress shows the update details of the plugin.
  • upgrader_post_install. Lastly, this is called after a plugin is successfully installed.

We are going to hook into these filters, then push our data into the results to make WordPress think that our plugin is in the WordPress plugin repository. The data we will put in will come from our GitHub repo, and should mimic the data given out by the plugin repository.

Setting Up the GitHub Project

Before we go ahead with coding, let's first talk about GitHub and how we'll use it to give out the data that we need to feed WordPress.

You will need either a private or a public GitHub repository. Your repo should contain all of your plugin files, and not a compressed copy of your plugin.

We will be using a cool feature of GitHub called Releases.

GitHub Releases

The good thing about releases is that it gets the current codebase in the repository and creates a downloadable zip file for each specific release. We can tell WordPress to download this zip file when updating our plugin.

Another good thing about Releases is that we can put in our plugin update details in the release notes. We can then parse this and display it in the lightbox WordPress shows for plugin update details. We can go further and even allow GitHub markdown for our changelog.

GitHub Releases

When it's time to deploy an update to our plugin, follow the formatting in the image above when you are creating a new release:

  • Tag Name: Plugin version (just the number)
  • Release notes: The description of the update
You can read more about GitHub Releases in their article Release Your Software

Creating our Updater Class

Now it's time to code our plugin!

First, we create the starting point for our class:

This is the class structure we're going to use. We just mainly defined all the functions we're going to use and created our filter hooks. This class does nothing as of the moment, except to assign values to class properties.

The Constructor Arguments

For our class to execute, we will need a few arguments:

  • $pluginFile: We will be calling this class from our main plugin script, this should have the value __FILE__. We will be getting details about our plugin from this later on.
  • $gitHubUsername: Your GitHub username
  • $gitHubProjectName: Your GitHub repository name
  • $accessToken: An access token that will allow us to view the details of a private GitHub repo. If your project is hosted in a public GitHub repo, just leave this blank.

Now let's fill up the functions of our class with some code.

You can learn more about creating access tokens from GitHub help. You can also read up on how the GitHub API works.

The initPluginData Function

This is the simplest function in our class. We will be needing our plugin's slug and other information throughout the rest of the script, so we're putting the necessary calls in a function for convenience.

The getRepoReleaseInfo Function

This function is all about communicating with GitHub to get our release information. We will use the GitHub API to get details regarding our latest release. Then store everything that we get in our githubAPIResult property for future processing.

The pre_set_site_transient_update_plugins filter is called twice by WordPress, once when it checks for plugin updates, then another after it gets results. Since we will be using this function in that filter, we would be querying the GitHub API twice. We just need to get information from GitHub once:

Next, we will be using the GitHub API to get information about our releases:

Lastly, we'll only keep the data for latest release of the plugin:

Now we can get our plugin data from GitHub. We will be parsing this data in the succeeding functions.

The setTransitent Function

This function is called when WordPress checks for plugin updates. Our job here is to use our GitHub release data to provide information for our plugin update.

The first thing we do is to check whether WordPress has already checked for plugin updates before. If it has, then we don't have to run the rest of the function again.

Next, we will get the plugin information that we are going to use:

After calling these two functions, we can check our local plugin's version from the version (the tag name) found in GitHub. We can use PHP's convenient version_compare function to compare the two values:

Lastly, there is a plugin update available, we need to prompt the admin to display an update notification. We do this by filling up the $transient variable with our updated plugin information.

After this function processes our GitHub information, our admin would be able to display notifications in the plugin admin page:

Plugin Update Notification

The setPluginInfo Function

This function's purpose is to gather details regarding the updated plugin from the release notes. All these information will be displayed inside a lightbox when the View version x.x details link is clicked.

First, let's get our plugin information:

Next we check whether or not it is out time to display anything. We can check if we are trying to load information for our current plugin by checking the slug:

To display our plugin details, we need to add our plugin information manually to the $response variable, normally this variable would be populated with the results from the WordPress plugin repository:

So far we've added our plugin details, but we haven't yet parsed our release notes from our GitHub release. Let's check what we have in our release notes:

GitHub Releases

In the release notes, we have specified three details regarding our release: our changelog, followed by the minimum required WordPress version, then the latest WordPress version the plugin was tested in. We're going to parse this text and extract these values.

Since we're hosting our plugin in GitHub, it would be nice if we can have the ability to use GitHub markdown in our release notes. I'm going to use a PHP class called ParseDown to convert the markdown text to HTML:

We're also going to create tabs in the lightbox to make it conform with how WordPress repository hosted plugins display their information. One will be for the plugin description and the other will be for our changelog:

Finally we're going to extract the values for the requires and tested:

The postInstall Function

This last function we'll deal with performing additional processes that we need to fully install our plugin after it is downloaded.

When creating a release for our GitHub repo, it automatically creates a zip file for that specific release. The filename for the zip file is generated by GitHub with the format reponame-tagname.zip. This also contains a directory where our plugin files are located. Similarly, the directory name for this also follows format reponame-tagname.

Normally, when WordPress downloads and unzips a plugin archive, the plugin directory name doesn't change. If you're plugin's directory is my-awesome-plugin, after deleting the old plugin files and unzipping the updated one, you're directory would still be named my-awesome-plugin. But since GitHub changes our plugin's directory name every time we deploy a release, WordPress won't be able to find our plugin. It would still be able to install it, but it won't be able to re-activate it. We can fix this issue by renaming the new directory to match the old one.

First thing's first:

Next we have to check whether our plugin is currently activated, so we can re-activate it afterwards:

Now we rename our updated plugin directory to match the old one. We're using the function move here, but since we are specifying the same directory, it would be just like renaming it:

The last step would be to re-activate the plugin:

Calling Our GitHub Updater Class

Now that the class is finished, all that's left to do is to call it in our main plugin file:

Trying It Out

That's it! Just include this class and call it in your plugin, and it should start checking for updates automatically.

To test if it works, create a new release for your GitHub repo and follow the guidelines stated earlier:

GitHub Releases

Once you have created your release, you can force WordPress to check for updates by clicking the refresh button found in the admin bar.

Conclusion

I hope you learned a thing or two about how WordPress works and how the whole process of updating plugins is performed. You can download the complete script from the download links at the top of this article.

I hope you enjoyed this article. I highly appreciate any feedback, comments and suggestions. Share your thoughts below!

Tags:

Comments

Related Articles