Guide to WordPress plugin updates using GitHub as your private repository | Extraparse

Guide to WordPress plugin updates using GitHub as your private repository

February 27, 20253 min read508 words

Guide to implementing custom WordPress plugin updates using GitHub as your private repository.

Table of Contents

Author: Extraparse

Step 1: Set Up the Plugin Repository

Step 1: Set Up the Plugin Repository

  • Create a GitHub Repository:
  • Go to GitHub and create a private repository.
  • Name the repository after your plugin, e.g., my-plugin.
  • Prepare the Plugin Files:
  • Ensure your WordPress plugin files are ready with proper versioning in the plugin header:
    1/*
    2Plugin Name: My Plugin
    3Plugin URI: https://yourwebsite.com
    4Description: Custom WordPress plugin.
    5Version: 1.0.0
    6Author: Your Name
    7Author URI: https://yourwebsite.com
    8*/
  • Place all files in a folder matching your plugin’s slug, e.g., my-plugin/.
  • Push Files to GitHub:
  • Initialize Git in your plugin folder: git init git add . git commit -m "Initial commit"
  • Push the plugin to your GitHub repository:
  • git remote add origin https://github.com/yourusername/my-plugin.git git branch -M main git push -u origin main

Step 2: Tag Releases in GitHub

  • Create a New Release:
  • Go to your repository on GitHub.
  • Navigate to the Releases tab and click Draft a new release.
  • Use the version number as the tag, e.g., v1.0.0.
  • Upload a ZIP file of the plugin for this release.
  • Generate the Release URL:
  • After publishing the release, note the download link for the ZIP file. It will look like this: https://github.com/yourusername/my-plugin/releases/download/v1.0.0/my-plugin.zip

Step 3: Add Update Check Code to Your Plugin

Modify your plugin to check for updates on GitHub and download the new version.

Include Update Logic: Add the following code to your plugin’s main file:

1function my_plugin_check_for_updates($transient) {
2 if (empty($transient->checked)) {
3 return $transient;
4 }
5
6 $plugin_slug = 'my-plugin/my-plugin.php'; // Replace with your plugin's slug
7 $current_version = '1.0.0'; // Replace with your current version
8
9 // Fetch update information from GitHub
10 $response = wp_remote_get('https://raw.githubusercontent.com/yourusername/my-plugin/main/update.json');
11 if (is_wp_error($response)) {
12 return $transient;
13 }
14
15 $data = json_decode(wp_remote_retrieve_body($response), true);
16 if (version_compare($current_version, $data['new_version'], '<')) {
17 $transient->response[$plugin_slug] = (object) [
18 'slug' => $plugin_slug,
19 'plugin' => $plugin_slug,
20 'new_version' => $data['new_version'],
21 'package' => $data['download_url'],
22 'tested' => $data['tested'],
23 'requires' => $data['requires'],
24 ];
25 }
26
27 return $transient;
28}
29add_filter('pre_set_site_transient_update_plugins', 'my_plugin_check_for_updates');

Create an update.json File:

Add this file to your GitHub repository and update its contents for each release:

1{
2 "new_version": "1.0.1",
3 "download_url": "https://github.com/yourusername/my-plugin/releases/download/v1.0.1/my-plugin.zip",
4 "requires": "5.0",
5 "tested": "6.4"
6}

Ensure Proper Permissions:

Use an access token if the repository is private. Generate a GitHub personal access token and include it in the wp_remote_get request:

1$response = wp_remote_get('https://raw.githubusercontent.com/yourusername/my-plugin/main/update.json', [
2 'headers' => [
3 'Authorization' => 'token your_github_access_token'
4 ]
5]);

Step 4: Automate Updates

To automate updates:

  • Increment the Version: Update the Version in your plugin’s header and update.json file for each new release.
  • Draft a New Release on GitHub: Tag the new release, upload the ZIP file, and publish it.

Step 5: Test the Update System

Install the plugin on a WordPress test site.

Verify that:

  • The plugin detects the update in the WordPress admin.
  • The update installs successfully.

Step 6: Deploy to Live Sites

Once tested, deploy the plugin to your live sites, and it will fetch updates automatically when you push new releases.

xtelegramfacebooktiktoklinkedin
Author: Extraparse

Comments

You must be logged in to comment.

Loading comments...