Creating a Plugin Update Checker System for WordPress

Posted on 18th June 2023

Introduction

As a WordPress plugin developer, it is important to provide your users with an update checker system so they can easily update your plugin when a new version is released. This tutorial will show you how to create a simple update checker system for your WordPress plugins.

Creating the Plugin Update Checker

The first thing you need to do is to create a new file called “plugin-update-checker.php” in your plugin’s folder. In this file, you will need to include the following code:

<?php

// Include the update checker class
include 'plugin-update-checker.php';

// Instantiate the update checker class
$myUpdateChecker = new PluginUpdateChecker(
    'https://example.com/path/to/your/plugin.json',
    __FILE__,
    'your-plugin-slug'
);

?>

Replace “https://example.com/path/to/your/plugin.json” with the URL of your plugin’s JSON file. This JSON file will contain the information about your plugin’s latest version, changelog, etc. You can learn more about the plugin JSON file here.

Replace “your-plugin-slug” with the slug of your plugin. This slug is used to identify your plugin in the WordPress Plugin Directory. You can find your plugin’s slug by going to its page in the directory (e.g. https://wordpress.org/plugins/your-plugin-slug/).

Once you have added the above code to your plugin, the update checker class will automatically check for updates to your plugin and display a notice to the user if an update is available.

Adding More Functionality to the Plugin Update Checker

The plugin update checker class provides a number of methods that you can use to add more functionality to your update checker system. For example, you can use the “addQueryArgFilter” method to add custom query parameters to the update checker URL. This can be useful if you want to track how many users are using the update checker system. To do this, you would need to add the following code to your plugin:

$myUpdateChecker->addQueryArgFilter(function($queryArgs) {
    $queryArgs['utm_source'] = 'update-checker';
    $queryArgs['utm_medium'] = 'plugin';
    return $queryArgs;
});

This code will add the “utm_source” and “utm_medium” query parameters to the update checker URL. These query parameters can then be used in Google Analytics to track how many users are using the update checker system.

You can also use the “addResultFilter” method to filter the update checker’s response before it is processed. This can be useful if you want to change the format of the update checker’s JSON file. For example, you can use the following code to change the format of the JSON file:

$myUpdateChecker->addResultFilter(function($pluginInfo) {
    $pluginInfo->version = '1.0';
    $pluginInfo->download_url = 'https://example.com/path/to/your/plugin.zip';
    $pluginInfo->sections = array(
        'section1' => 'This is the content of section 1',
        'section2' => 'This is the content of section 2'
    );
    return $pluginInfo;
});

This code will change the format of the JSON file to the new format. You can learn more about the plugin update checker class here.

Conclusion

In this tutorial, you have learned how to create a simple plugin update checker system for your WordPress plugins. You have also learned how to add more functionality to the update checker system by using the plugin update checker class.

The first thing you need to do is create a directory on your server where you will store all the update information for your WordPress plugins. Next, create a file called update-checker.php and place the following code inside it:

plugin_file = $plugin_file;

$this->current_version = $current_version;

}

public function check_for_update() {

$request_url = ‘https://your-update-server.com/plugins/’ . basename($this->plugin_file) . ‘/update-checker.php’;

$request_args = array(

‘method’ => ‘POST’,

‘body’ => array(

‘plugin_file’ => $this->plugin_file,

‘current_version’ => $this->current_version

)

);

$response = wp_remote_post($request_url, $request_args);

if( !is_wp_error($response) && $response[‘response’][‘code’] == 200 ) {

$body = json_decode($response[‘body’]);

if( version_compare($this->current_version, $body->new_version, ‘new_version;

}

}

return false;

}

}

?>

In the code above, we first define a few variables that we will need later on. The $plugin_dir variable holds the path to the directory where our plugin is stored. The $plugin_file variable holds the path to our plugin’s main file. The $current_version variable holds the version number of our plugin that is currently installed.

Next, we create a new UpdateChecker object and pass it our $plugin_file and $current_version variables. The UpdateChecker class is where most of the work is done.

Inside the UpdateChecker class, we first create two private variables that we will need later on. The $plugin_file variable holds the path to our plugin’s main file. The $current_version variable holds the version number of our plugin that is currently installed.

Next, we create a public __construct() method that takes two parameters. The first parameter is the path to our plugin’s main file. The second parameter is the version number of our plugin that is currently installed. We set the two private variables that we created earlier to the values of these parameters.

After the __construct() method, we create a public check_for_update() method. This is the method that actually checks for updates to our plugin.

Inside the check_for_update() method, we first define the $request_url variable. This variable holds the URL of the update checker script on our server. We then define the $request_args variable. This variable holds an array of arguments that we will pass to the wp_remote_post() function.

The $request_args array contains two items. The first item is the ‘method’ item. This tells the wp_remote_post() function that we want to use the POST method when making our request. The second item is the ‘body’ item. This is an array of data that we want to POST to our update checker script. The ‘body’ array contains two items. The first item is the ‘plugin_file’ item. This tells our update checker script the path to our plugin’s main file. The second item is the ‘current_version’ item. This tells our update checker script the version number of our plugin that is currently installed.

After we have defined the $request_url and $request_args variables, we can make our request to our update checker script using the wp_remote_post() function. We pass the $request_url and $request_args variables to the wp_remote_post() function.

The wp_remote_post() function returns a WP_Error object if there was an error making the request or an array if the request was successful. We use the is_wp_error() function to check if the request was successful.

If the request was successful, we then check the ‘response’ array for a ‘code’ item. The ‘code’ item contains the HTTP status code of the response. If the HTTP status code is 200, then we know that the request was successful.

If the request was successful, we then decode the body of the response using the json_decode() function. The body of the response contains a JSON encoded string. The json_decode() function decodes the JSON string and returns an object.

We then check if the ‘new_version’ item of the decoded body object is greater than the $current_version variable. If it is, then we return the ‘new_version’ item. If it isn’t, then we return false.

If the request wasn’t successful or the HTTP status code wasn’t 200, then we return false.

That’s it! You should now have a working plugin update checker system for your WordPress plugins.