Auto update of WordPress plugin – Implementation

For the auto renewal of WordPress plugins our plugin includes a sample folder for use.

For plugin, You will find a folder named – Sparkle-edd-sample-plugin inside samples folder.
The folder contains 2 files

  1. sparkle-edd-sample-plugin.php
  2. SparkleEDDSamplePluginUpdater.php

sparkle-edd-sample-plugin.php file contains the sample codes that is required for the updates of the latest plugin.

defined( 'SEDD_SHOP_BASE_URL' ) or define( 'SEDD_SHOP_BASE_URL', '' ); // please set the value '' to your site url where your store is located and our plugin - Sparkle EDD Licensing is being used for software licensing.

defined( 'SEDD_PRODUCT_ID' ) or define( 'SEDD_PRODUCT_ID', 'yourproductid' ); // Please set the download id exactly from the downloads product id.

defined( 'SEDD_PRODUCT_NAME' ) or define( 'SEDD_PRODUCT_NAME', 'YOURPRODUCTNAME' ); // Please set your product name here

defined( 'SEDD_PLUGIN_VERSION' ) or define( 'SEDD_PLUGIN_VERSION', 'YOURPLUGINVERSION' ); // Please set your plugin version number here

Now for the notification of update available and one click update of a plugin you need to include the updater class in your plugin like this.

if( !class_exists( 'SparkleEDDSamplePluginUpdater' ) ) {
include_once( dirname( FILE ) . '/SparkleEDDSamplePluginUpdater.php' );

After inclusion of the updater class file. Please include the following function and action in your plugin file.

function seddl_sample_plugin_updater(){
	$plugin_current_version = SEDD_PLUGIN_VERSION;
	$plugin_remote_path = SEDD_SHOP_BASE_URL."/wp-json/sparkleddl/v1/software_update/";
	$plugin_slug = plugin_basename( __FILE__ );

	$license_user = 'user';
	$license_key = get_option( 'sedd_sample_license_key' );

	new SparkleEDDSamplePluginUpdater ( $plugin_current_version, $plugin_remote_path, $plugin_slug, $license_user, $license_key );
add_action( 'admin_init', 'seddl_sample_plugin_updater' );

In the above function you don’t need to modify any variables. The parameters passed to the updater class are as follows:

$plugin_current_version – This is the variable for the current version of a plugin which will be obtained automatically from constant SEDD_PLUGIN_VERSION.
$plugin_remote_path – This is remote url to get the json response for software updates.
$plugin_slug – The slug of the plugin which will be obtained automatically by plugin_basename( FILE )
$license_key – It will be obtained from the license options settings.

How to create settings page for activating the license key in the plugin for receving the updates of a plugin?
In order to receive continuous updates of a plugin(if available) the license key should be active and valid. So for the validation or activation of a license key you need to setup a settings page which is also included in the plugin.

function sparkle_sample_menu(){
add_plugins_page( "Sparkle License", "Sparkle License", 'manage_options', SEDD_PLUGIN_LICENSE_PAGE, 'sedd_license_page' );
add_action( 'admin_menu', 'sparkle_sample_menu' );

You need to use the reference code above to create a menu which will create a page to activate/validate the license key. The sedd_license_page function will be used for activation and validation of the license key. You can just copy the function and can use it. You can change the options name as per your need.

In this page at first the user need to save the license key first so that the license key will be stored in options table then only they will be able to activate the license key.

Now when use click on activate button the response will be returned based on license key saved.

Powered by BetterDocs