{"id":230,"date":"2025-04-01T15:03:07","date_gmt":"2025-04-01T15:03:07","guid":{"rendered":"https:\/\/www.kwwd.co.uk\/blog\/?p=230"},"modified":"2025-04-01T15:03:07","modified_gmt":"2025-04-01T15:03:07","slug":"wordpress-update-plugin-via-git","status":"publish","type":"post","link":"https:\/\/www.kwwd.co.uk\/blog\/wordpress-update-plugin-via-git\/","title":{"rendered":"WordPress: Update Plugin Via GIT"},"content":{"rendered":"<p>Create a class to handle updates<\/p>\n<p>Add this to your plugin\u2019s main PHP file or a separate updater file:<\/p>\n<pre><code class=\"language-php line-numbers\">\r\n&lt;?php\r\n\r\nclass GitHub_Plugin_Updater {\r\n    private $slug;\r\n    private $plugin_file;\r\n    private $github_user;\r\n    private $github_repo;\r\n    private $github_api_url;\r\n\r\n    public function __construct($plugin_file, $github_user, $github_repo) {\r\n        $this->plugin_file = $plugin_file;\r\n        $this->slug = plugin_basename($plugin_file);\r\n        $this->github_user = $github_user;\r\n        $this->github_repo = $github_repo;\r\n        $this->github_api_url = \"https:\/\/api.github.com\/repos\/{$github_user}\/{$github_repo}\/releases\/latest\";\r\n\r\n        add_filter('pre_set_site_transient_update_plugins', [$this, 'check_for_update']);\r\n        add_filter('plugins_api', [$this, 'plugin_info'], 10, 3);\r\n        add_filter('upgrader_package_options', [$this, 'modify_zip_download'], 10, 1);\r\n    }\r\n\r\n    \/**\r\n     * Check GitHub for updates\r\n     *\/\r\n    public function check_for_update($transient) {\r\n        if (empty($transient->checked)) {\r\n            return $transient;\r\n        }\r\n\r\n        \/\/ Get plugin data\r\n        $plugin_data = get_plugin_data($this->plugin_file);\r\n        $current_version = $plugin_data['Version'];\r\n\r\n        \/\/ Get latest release from GitHub\r\n        $response = wp_remote_get($this->github_api_url, ['headers' => ['User-Agent' => 'WordPress']]);\r\n        if (is_wp_error($response)) {\r\n            return $transient;\r\n        }\r\n\r\n        $release_data = json_decode(wp_remote_retrieve_body($response));\r\n        if (!$release_data || !isset($release_data->tag_name)) {\r\n            return $transient;\r\n        }\r\n\r\n        \/\/ Debug: Log the release assets to check the download URL\r\n        \/\/error_log('Release Assets: ' . json_encode($release_data->assets));\t\t\r\n\t\t\/\/error_log('Browser URL: ' . $release_data->assets[0]->browser_download_url);\r\n\r\n        \/\/ Get the version tag from GitHub\r\n        $new_version = $release_data->tag_name;  \/\/ Get the tag name from GitHub\r\n        $new_version = str_replace('v', '', $new_version);  \/\/ Remove 'v' if present\r\n\r\n        if (version_compare($current_version, $new_version, '&lt;')) {\r\n            $transient->response[$this->slug] = (object) [\r\n                'slug'        => $this->slug,\r\n                'new_version' => $new_version,\r\n                'package'     => $release_data->assets[0]->browser_download_url, \/\/ Use the correct zipball URL\r\n                'url'         => $release_data->html_url,\r\n            ];\r\n        }\r\n\r\n        return $transient;\r\n    }\r\n\r\n    \/**\r\n     * Provide plugin details in the update popup\r\n     *\/\r\n    public function plugin_info($false, $action, $args) {\r\n        if ($action !== 'plugin_information' || $args->slug !== $this->slug) {\r\n            return $false;\r\n        }\r\n\r\n        $response = wp_remote_get($this->github_api_url, ['headers' => ['User-Agent' => 'WordPress']]);\r\n        if (is_wp_error($response)) {\r\n            return $false;\r\n        }\r\n\r\n        $release_data = json_decode(wp_remote_retrieve_body($response));\r\n        if (!$release_data || !isset($release_data->tag_name)) {\r\n            return $false;\r\n        }\r\n\r\n        return (object) [\r\n            'name'          => $plugin_data['Name'],\r\n            'slug'          => $this->slug,\r\n            'version'       => $release_data->tag_name,\r\n            'author'        => 'Your Name',\r\n            'homepage'      => $release_data->html_url,\r\n            'sections'      => ['description' => $release_data->body],\r\n            'download_link' => $release_data->assets[0]->browser_download_url, \/\/ Use the correct zipball URL\r\n        ];\r\n    }\r\n\r\n    \/**\r\n     * Modify the package URL to point to the correct GitHub zip file\r\n     *\/\r\n    public function modify_zip_download($options) {\r\n        if (!empty($options['hook_extra']['plugin']) && $options['hook_extra']['plugin'] === $this->slug) {\r\n            $response = wp_remote_get($this->github_api_url, ['headers' => ['User-Agent' => 'WordPress']]);\r\n            if (!is_wp_error($response)) {\r\n                $release_data = json_decode(wp_remote_retrieve_body($response));\r\n                if ($release_data && isset($release_data->assets[0]->browser_download_url)) {\r\n                    $options['package'] = $release_data->assets[0]->browser_download_url; \/\/ Ensure correct URL is used\r\n                }\r\n            }\r\n        }\r\n        return $options;\r\n    }\r\n}\r\n\r\n?>\r\n\r\n<\/code><\/pre>\n<p>Initialize the Updater in Your Plugin<\/p>\n<p>In your plugin\u2019s main file (e.g., my-plugin.php), add:<\/p>\n<pre><code class=\"language-php line-numbers\">\r\nif (is_admin()) {\r\n    require_once plugin_dir_path(__FILE__) . 'github-updater.php';\r\n    new GitHub_Plugin_Updater(__FILE__, 'your-github-username', 'your-plugin-repository');\r\n}\r\n<\/code><\/pre>\n<p>Replace &#8216;your-github-username&#8217; and &#8216;your-plugin-repository&#8217; with your actual GitHub username and repo name.<\/p>\n<p>Ensure Your GitHub Repository is Set Up Correctly<\/p>\n<p>&#8211; Create tagged releases (e.g., v1.0.1)<br \/>\n&#8211; Attach a ZIP file of the plugin as a release asset<br \/>\n&#8211; Make sure the release follows semantic versioning (e.g., v1.0.1 instead of 1.0.1)<\/p>\n<p>Ensure your plugin has the pre-requiste header code:<\/p>\n<pre><code class=\"language-php line-numbers\">\r\n\/**\r\n * Plugin Name: KWWD Pinterest Designer\r\n * Description: Create a Pinterest Image On The Fly\r\n * Version: 1.0.1\r\n * Author: Katy Whitton\r\n * Requires at least: 5.0\r\n * Tested up to: 6.7.2\r\n * Requires PHP: 7.0\r\n *\/\r\n<\/code><\/pre>\n<p>Ensure that your release is public and tagged correctly with a version number<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Create a class to handle updates Add this to your plugin\u2019s main PHP file or a separate updater file: &lt;?php class GitHub_Plugin_Updater { private $slug; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[55,57,12],"class_list":["post-230","post","type-post","status-publish","format-standard","hentry","category-wordpress","tag-plugin","tag-updater","tag-wordpress"],"_links":{"self":[{"href":"https:\/\/www.kwwd.co.uk\/blog\/wp-json\/wp\/v2\/posts\/230","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kwwd.co.uk\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kwwd.co.uk\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kwwd.co.uk\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kwwd.co.uk\/blog\/wp-json\/wp\/v2\/comments?post=230"}],"version-history":[{"count":0,"href":"https:\/\/www.kwwd.co.uk\/blog\/wp-json\/wp\/v2\/posts\/230\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.kwwd.co.uk\/blog\/wp-json\/wp\/v2\/media?parent=230"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kwwd.co.uk\/blog\/wp-json\/wp\/v2\/categories?post=230"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kwwd.co.uk\/blog\/wp-json\/wp\/v2\/tags?post=230"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}