Synopsis
- You have a plugin that you host on GitHub (must be public, not private)
- You want new releases for that plugin to show up in wp-admin
- You want to be able to “enable auto-updates” for that plugin in wp-admin
How
Since WordPress 5.8, we’ve been able to set the Update URI
in the plugin header. Setting that plugin URI allows us to add a filter to modify the update process based on the Update URI.
The Code
First, you need to add the Update URI
to your plugin header, i.e.:
<?php
/*
Plugin Name: Your Custom Plugin
Update URI: https://github.com/your-org/your-custom-plugin/
*/
Then, add a filter like this:
<?php
add_filter( 'update_plugins_github.com', 'self_update', 10, 4 );
/**
* Check for updates to this plugin
*
* @param array $update Array of update data.
* @param array $plugin_data Array of plugin data.
* @param string $plugin_file Path to plugin file.
* @param string $locales Locale code.
*
* @return array|bool Array of update data or false if no update available.
*/
function self_update( $update, array $plugin_data, string $plugin_file, $locales ) {
// only check this plugin
if ( 'your-custom-plugin/your-custom-plugin.php' !== $plugin_file ) {
return $update;
}
// already completed update check elsewhere
if ( ! empty( $update ) ) {
return $update;
}
// let's go get the latest version number from GitHub
$response = wp_remote_get(
'https://api.github.com/repos/your-org/your-custom-plugin/releases/latest',
array(
'user-agent' => 'YOUR_GITHUB_USERNAME',
)
);
if ( is_wp_error( $response ) ) {
return;
} else {
$output = json_decode( wp_remote_retrieve_body( $response ), true );
}
$new_version_number = $output['tag_name'];
$is_update_available = version_compare( $plugin_data['Version'], $new_version_number, '<' );
if ( ! $is_update_available ) {
return false;
}
$new_url = $output['html_url'];
$new_package = $output['assets'][0]['browser_download_url'];
error_log('$plugin_data: ' . print_r( $plugin_data, true ));
error_log('$new_version_number: ' . $new_version_number );
error_log('$new_url: ' . $new_url );
error_log('$new_package: ' . $new_package );
return array(
'slug' => $plugin_data['TextDomain'],
'version' => $new_version_number,
'url' => $new_url,
'package' => $new_package,
);
}
The Result
When WordPress checks if there are plugin updates available, the update now shows as being available. If you have auto-updates enabled, then the plugin will autoupdate.
