Creating a Plugin Update Notification System

Posted on 17th June 2023

As a WordPress developer, it’s important to keep your plugins up to date. Not only do you want to keep your users happy, but you also want to make sure your plugins are secure. In this article, we’ll show you how to create a plugin update notification system.

Step 1: Create a Plugin Update Class

The first thing you need to do is create a plugin update class. This class will be responsible for checking if there is a new version of your plugin available. If there is, it will notify the user and allow them to update the plugin.

class PluginUpdate {

private $current_version;

private $new_version;

private $plugin_slug;

private $plugin_file;

public function __construct( $current_version, $new_version, $plugin_slug, $plugin_file ) {

$this->current_version = $current_version;
$this->new_version = $new_version;
$this->plugin_slug = $plugin_slug;
$this->plugin_file = $plugin_file;

add_action( 'admin_init', array( $this, 'check_for_update' ) );
add_action( 'admin_notices', array( $this, 'display_update_notice' ) );
}

public function check_for_update() {

if ( version_compare( $this->current_version, $this->new_version, 'update_plugin();
}
}

public function update_plugin() {

$url = 'http://update.yourplugin.com/';

$args = array(
'body' => array(
'plugin' => $this->plugin_slug,
'version' => $this->new_version,
'file' => $this->plugin_file
)
);

$response = wp_remote_post( $url, $args );

if ( ! is_wp_error( $response ) ) {
$body = wp_remote_retrieve_body( $response );

$data = json_decode( $body );

if ( $data->success ) {
$this->current_version = $this->new_version;
update_option( 'plugin_version', $this->current_version );
}
}
}

public function display_update_notice() {

if ( version_compare( $this->current_version, $this->new_version, '<' ) ) {
echo '
'; echo 'There is a new version of the plugin available. Update now'; echo '
'; } } }

Step 2: Instantiate the Plugin Update Class

Once you have the plugin update class created, you need to instantiate it. You can do this by adding the following code to your plugin’s main file:

$current_version = '1.0.0';
$new_version = '1.0.1';
$plugin_slug = 'my-plugin';
$plugin_file = plugin_basename( __FILE__ );

$plugin_update = new PluginUpdate( $current_version, $new_version, $plugin_slug, $plugin_file );

Step 3: Create the Plugin Update Server

The next step is to create the plugin update server. This is where your plugin will check for updates. You can use any web server for this, but we recommend using NGINX. Once you have NGINX installed, you need to create a file called update.php in the root of your web server. The contents of this file should be:

<?php

$plugin = $_POST['plugin'];
$version = $_POST['version'];
$file = $_POST['file'];

if ( $plugin == 'my-plugin' && $version == '1.0.1' ) {
$url = 'https://downloads.wordpress.org/plugin/my-plugin.1.0.1.zip';

$args = array(
'body' => array(
'success' => true,
'url' => $url
)
);

echo json_encode( $args );
exit;
}

$args = array(
'body' => array(
'success' => false
)
);

echo json_encode( $args );
exit;

This file checks the plugin and version parameters that are being passed in. If the plugin is “my-plugin” and the version is “1.0.1”, it will return a JSON object with a success property set to true and a url property set to the URL of the plugin ZIP file. If the plugin or version don’t match, it will return a JSON object with a success property set to false.

Step 4: Upload the Plugin ZIP File

The final step is to upload the plugin ZIP file to your web server. You can do this using SFTP. Once the file is uploaded, you need to update the update.php file to point to the new plugin ZIP file. For example, if you’re updating from version 1.0.0 to 1.0.1, you would change the update.php file to the following:

<?php

$plugin = $_POST['plugin'];
$version = $_POST['version'];
$file = $_POST['file'];

if ( $plugin == 'my-plugin' && $version == '1.0.1' ) {
$url = 'https://downloads.wordpress.org/plugin/my-plugin.1.0.1.zip';

$args = array(
'body' => array(
'success' => true,
'url' => $url
)
);

echo json_encode( $args );
exit;
}

$args = array(
'body' => array(
'success' => false
)
);

echo json_encode( $args );
exit;

Now when your plugin checks for updates, it will download the new ZIP file from your server. Once it has the new ZIP file, it will extract it and update your plugin.

Conclusion

In this article, we showed you how to create a plugin update notification system. This system will keep your users up to date with the latest versions of your plugin and make sure your plugin is always secure. If you have any questions, please let us know in the comments.

To complete our update notification system, we need to do two things: first, we need to create a new table in our database to store information about plugin updates, and second, we need to modify our existing code to check for updates and display notifications to the user.

First, we’ll create a new table in our database. This table will have three columns: plugin_name, current_version, and new_version. The plugin_name column will store the name of the plugin that has an update available, the current_version column will store the version of the plugin that the user currently has installed, and the new_version column will store the latest version of the plugin that is available.

Next, we’ll need to modify our existing code to check for updates and display notifications to the user. We’ll do this by adding a new function to our PluginUpdateChecker class. This function will take two parameters: the name of the plugin to check for updates, and the current version of the plugin.

The function will first query the database for any updates that are available for the plugin. If there are no updates available, the function will return false. If there are updates available, the function will return an array of update objects. Each update object will contain the following information:

plugin_name: the name of the plugin that has an update available

current_version: the version of the plugin that the user currently has installed

new_version: the latest version of the plugin that is available

Once we have our array of update objects, we can loop through it and display a notification to the user for each update that is available.

That’s it! With our update notification system in place, our users will always be up-to-date on the latest versions of our plugins.