Create a class to handle updates
Add this to your plugin’s main PHP file or a separate updater file:
<?php
class GitHub_Plugin_Updater {
private $slug;
private $plugin_file;
private $github_user;
private $github_repo;
private $github_api_url;
public function __construct($plugin_file, $github_user, $github_repo) {
$this->plugin_file = $plugin_file;
$this->slug = plugin_basename($plugin_file);
$this->github_user = $github_user;
$this->github_repo = $github_repo;
$this->github_api_url = "https://api.github.com/repos/{$github_user}/{$github_repo}/releases/latest";
add_filter('pre_set_site_transient_update_plugins', [$this, 'check_for_update']);
add_filter('plugins_api', [$this, 'plugin_info'], 10, 3);
add_filter('upgrader_package_options', [$this, 'modify_zip_download'], 10, 1);
}
/**
* Check GitHub for updates
*/
public function check_for_update($transient) {
if (empty($transient->checked)) {
return $transient;
}
// Get plugin data
$plugin_data = get_plugin_data($this->plugin_file);
$current_version = $plugin_data['Version'];
// Get latest release from GitHub
$response = wp_remote_get($this->github_api_url, ['headers' => ['User-Agent' => 'WordPress']]);
if (is_wp_error($response)) {
return $transient;
}
$release_data = json_decode(wp_remote_retrieve_body($response));
if (!$release_data || !isset($release_data->tag_name)) {
return $transient;
}
// Debug: Log the release assets to check the download URL
//error_log('Release Assets: ' . json_encode($release_data->assets));
//error_log('Browser URL: ' . $release_data->assets[0]->browser_download_url);
// Get the version tag from GitHub
$new_version = $release_data->tag_name; // Get the tag name from GitHub
$new_version = str_replace('v', '', $new_version); // Remove 'v' if present
if (version_compare($current_version, $new_version, '<')) {
$transient->response[$this->slug] = (object) [
'slug' => $this->slug,
'new_version' => $new_version,
'package' => $release_data->assets[0]->browser_download_url, // Use the correct zipball URL
'url' => $release_data->html_url,
];
}
return $transient;
}
/**
* Provide plugin details in the update popup
*/
public function plugin_info($false, $action, $args) {
if ($action !== 'plugin_information' || $args->slug !== $this->slug) {
return $false;
}
$response = wp_remote_get($this->github_api_url, ['headers' => ['User-Agent' => 'WordPress']]);
if (is_wp_error($response)) {
return $false;
}
$release_data = json_decode(wp_remote_retrieve_body($response));
if (!$release_data || !isset($release_data->tag_name)) {
return $false;
}
return (object) [
'name' => $plugin_data['Name'],
'slug' => $this->slug,
'version' => $release_data->tag_name,
'author' => 'Your Name',
'homepage' => $release_data->html_url,
'sections' => ['description' => $release_data->body],
'download_link' => $release_data->assets[0]->browser_download_url, // Use the correct zipball URL
];
}
/**
* Modify the package URL to point to the correct GitHub zip file
*/
public function modify_zip_download($options) {
if (!empty($options['hook_extra']['plugin']) && $options['hook_extra']['plugin'] === $this->slug) {
$response = wp_remote_get($this->github_api_url, ['headers' => ['User-Agent' => 'WordPress']]);
if (!is_wp_error($response)) {
$release_data = json_decode(wp_remote_retrieve_body($response));
if ($release_data && isset($release_data->assets[0]->browser_download_url)) {
$options['package'] = $release_data->assets[0]->browser_download_url; // Ensure correct URL is used
}
}
}
return $options;
}
}
?>
Initialize the Updater in Your Plugin
In your plugin’s main file (e.g., my-plugin.php), add:
if (is_admin()) {
require_once plugin_dir_path(__FILE__) . 'github-updater.php';
new GitHub_Plugin_Updater(__FILE__, 'your-github-username', 'your-plugin-repository');
}
Replace ‘your-github-username’ and ‘your-plugin-repository’ with your actual GitHub username and repo name.
Ensure Your GitHub Repository is Set Up Correctly
– Create tagged releases (e.g., v1.0.1)
– Attach a ZIP file of the plugin as a release asset
– Make sure the release follows semantic versioning (e.g., v1.0.1 instead of 1.0.1)
Ensure your plugin has the pre-requiste header code:
/**
* Plugin Name: KWWD Pinterest Designer
* Description: Create a Pinterest Image On The Fly
* Version: 1.0.1
* Author: Katy Whitton
* Requires at least: 5.0
* Tested up to: 6.7.2
* Requires PHP: 7.0
*/
Ensure that your release is public and tagged correctly with a version number
Disclaimer: The code on this website is provided "as is" and comes with no warranty. The author of this website does not accept any responsibility for issues arising from the use of code on this website. Before making any significant changes, ensure you take a backup of all files and do not work directly on a live/production website without thoughly testing your changes first.