Auto updates for WordPress themes and plugins from private GitLab repos

The last few days I worked on the following scenario: a WordPress plugin or theme gets its update from a private GitLab repo and checks the Git tags for a new version. The result is a WordPress plugin, which also can be integrated into a plugin or theme directly.

You can find the plugin on GitHub. It is pretty much my first class orientated PHP code (perhaps even object orientated(?)), and I would be happy about feedback and suggestions to improve it — here in the comments or in the GitHub repo 🙂 I get inspiration for namespaces and tags in code comments from a repo by Alain Schlesser.

Usage as a WordPress plugin

The themes tab of the settings page from the GitLab updater. (Screenshot: own installation)

But now to the tool. You can use it — as already said — as a plugin or included into the theme or plugin which should get updates. If you use it as a plugin, you get a new settings page under Settings › GitLab Updater, which shows a list with all your themes and one with all your plugins, and for each of that three fields (above in the screenshot you can see the theme tab):

  • Access token is the GitLab access token, which needs the api and read_registry scope. It may be the safest way to create that token for a user with reporter capabilities, who has only access to that particular repo.
  • GitLab URL is the URL of the GitLab installation, for example,
  • Repo is the ID of the repo like shown in the URL if you open it in GitLab: username/project or group/project.

Implemented into a theme or plugin

If you only want to use the feature for one theme or plugin in an installation, you can implement it directly (also possible with multiple usages per installation — for that you need to change the namespace to avoid a conflict with multiple classes of the same name).

In a theme

To use it in a theme, grab the src/ThemeUpdater.php and the src/UpdaterBase.php, throw it into your theme and run the following code from the functions.php (or another file which gets included into the functions.php):

/** * Include the file with the ThemeUpdater class. */ require_once 'wp-gitlab-updater/theme-updater.php'; /** * Init the theme updater. */ new Moenus\GitLabUpdater\ThemeUpdater( [ 'slug' => 'SlugOfTheTheme', 'access_token' => 'YourGitLabAccessToken', 'gitlab_url' => 'URLtoGitLabInstall', 'repo' => 'RepoIdentifier', ] );
Code language: PHP (php)

The params are the same as above — the only additional one is the theme’s slug.

In a plugin

For usage of the script inside a plugin, you need the src/PluginUpdater.php and the src/UpdaterBase.php. This is how the call can look like:

/** * Include the file with the PluginUpdater class. */ require_once 'wp-gitlab-updater/plugin-updater.php'; /** * Init the plugin updater with the plugin base name. */ new Moenus\GitLabUpdater\PluginUpdater( [ 'slug' => 'SlugOfPlugin', 'plugin_base_name' => 'BaseNameOfThePlugin', 'access_token' => 'YourGitLabAccessToken', 'gitlab_url' => 'URLtoGitLabInstall', 'repo' => 'RepoIdentifier', ] );
Code language: PHP (php)

One more param here: plugin_base_name is the plugin directory and the name of the main file — for example, svg-social-menu/svg-social-menu.php.

1 reaction on »Auto updates for WordPress themes and plugins from private GitLab repos«

Leave a Reply

Your email address will not be published. Required fields are marked *