A reusable WordPress plugin updater class that enables automatic updates from GitHub repositories. Built on top of the yahnis-elsts/plugin-update-checker
library.
- ✅ Automatic updates from GitHub releases and branches
- ✅ Release asset filtering with regex patterns
- ✅ Custom branch support for development/staging
- ✅ Simple static factory methods for easy setup
- ✅ Built-in error handling and debug logging
- ✅ Flexible configuration options
Copy class-github-plugin-updater.php
to your plugin directory and include it:
if ( ! class_exists( 'Soderlind\WordPress\GitHub_Plugin_Updater' ) ) { require_once 'class-github-plugin-updater.php'; } // Basic setup (most common) $updater = \Soderlind\WordPress\GitHub_Plugin_Updater::create( 'https://github.com/username/plugin-name', __FILE__, 'plugin-name' );
That's it! Your plugin will now check for updates from the specified GitHub repository.
Parameter | Required | Description | Example |
---|---|---|---|
github_url | Yes | GitHub repository URL | 'https://github.com/username/plugin-name' |
plugin_file | Yes | Path to main plugin file | __FILE__ |
plugin_slug | Yes | Plugin slug for WordPress | 'my-awesome-plugin' |
branch | No | Git branch to check for updates (default: 'master' ) | 'main' , 'develop' |
name_regex | No | Regex pattern for release assets | '/plugin-name\.zip/' |
enable_release_assets | No | Whether to enable release assets | true if name_regex provided |
Note: When
branch
is set tomaster
, the updater prioritizes releases and tags before falling back to the branch itself.
$updater = \Soderlind\WordPress\GitHub_Plugin_Updater::create( 'https://github.com/username/plugin-name', __FILE__, 'plugin-name' );
$updater = \Soderlind\WordPress\GitHub_Plugin_Updater::create( 'https://github.com/username/plugin-name', __FILE__, 'plugin-name', 'develop' // Branch name );
$updater = \Soderlind\WordPress\GitHub_Plugin_Updater::create_with_assets( 'https://github.com/username/plugin-name', __FILE__, 'plugin-name', '/plugin-name\.zip/' // Regex pattern for zip file );
$updater = new \Soderlind\WordPress\GitHub_Plugin_Updater([ 'github_url' => 'https://github.com/username/plugin-name', 'plugin_file' => __FILE__, 'plugin_slug' => 'plugin-name', 'branch' => 'main', 'name_regex' => '/plugin-name-v[\d\.]+\.zip/', 'enable_release_assets' => true, ]);
To automatically create release assets, add this workflow to your repository at .github/workflows/on-release-add.zip.yml
:
name: On Release, Build release zip on: release: types: [published] jobs: build: name: Build release zip runs-on: ubuntu-latest permissions: contents: write steps: - name: Checkout uses: actions/checkout@v4 - name: Build plugin run: composer install --no-dev - name: Archive Release uses: thedoctor0/zip-release@b57d897cb5d60cb78b51a507f63fa184cfe35554 with: type: 'zip' filename: 'your-plugin-name.zip' # Change this exclusions: '*.git* .editorconfig composer* *.md package.json package-lock.json' - name: Release uses: softprops/action-gh-release@c95fe1489396fe8a9eb87c0abf8aa5b2ef267fda env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} with: files: your-plugin-name.zip # Change this tag_name: ${{ github.event.release.tag_name }}
- Update
filename
andfiles
to match your plugin name - Modify
exclusions
to include/exclude specific files - Adjust the build step for your project needs (npm, webpack, etc.)
This creates downloadable assets at URLs like:
https://github.com/username/plugin-name/releases/latest/download/plugin-name.zip
// In your main plugin file define( 'MY_PLUGIN_FILE', __FILE__ ); require_once plugin_dir_path( __FILE__ ) . 'class-github-plugin-updater.php'; $updater = \Soderlind\WordPress\GitHub_Plugin_Updater::create( 'https://github.com/myusername/my-awesome-plugin', MY_PLUGIN_FILE, 'my-awesome-plugin' );
$updater = \Soderlind\WordPress\GitHub_Plugin_Updater::create( 'https://github.com/company/enterprise-plugin', __FILE__, 'enterprise-plugin', 'develop' // Use development branch );
$updater = \Soderlind\WordPress\GitHub_Plugin_Updater::create_with_assets( 'https://github.com/company/premium-plugin', __FILE__, 'premium-plugin', '/premium-plugin-v[\d\.]+\.zip/' // Match versioned zip files );
If you need to avoid naming conflicts, extend or wrap the class:
namespace YourCompany\YourPlugin; class Your_Plugin_Updater extends \Soderlind\WordPress\GitHub_Plugin_Updater { // Inherit all functionality, customize as needed }
namespace YourCompany\YourPlugin; class Your_Plugin_Updater { private $updater; public function __construct() { $this->updater = \Soderlind\WordPress\GitHub_Plugin_Updater::create( 'https://github.com/yourcompany/your-plugin', YOUR_PLUGIN_FILE, 'your-plugin' ); } }
- Use constants for plugin file paths:
define( 'MY_PLUGIN_FILE', __FILE__ )
- Prefer static factory methods like
::create()
for simpler setup - Test thoroughly with different branches and release patterns
- Document your configuration for future reference
- Consider namespacing to avoid conflicts with other plugins
The updater includes built-in error handling:
- Parameter validation on initialization
- Exception graceful handling during update checks
- Debug logging when
WP_DEBUG
is enabled - Graceful degradation if updater setup fails
This updater requires the yahnis-elsts/plugin-update-checker
library:
composer require yahnis-elsts/plugin-update-checker
Or download manually from: https://github.com/YahnisElsts/plugin-update-checker
MIT (same as the original library)