Automatic Updating of WordPress Themes and Plugins
Selling your WordPress theme or plugin on Sellcodes has many advantages, one of them being automatic updates: After users bought your product on Sellcodes, they can update it with a click of a button - just like with free plugins and themes in the WordPress directory!
Enabling this is quite simple - after you've set up your offer and configured the licensing options, there is just a small code snippet and a file you need to include in your theme or plugin files.
There are two components to the updater:
- The sellcodes_plugin_updater.php file. This contains the class that handles the updating process.
- The code snippet that you place somewhere in your plugin that loads the updater provided by the sellcodes_plugin_updater.php class file.
Step 1 - Define the updater constants
In your main plugin file, preferably near the top, or where you have other constants defined, add the following code:
// this is the URL our updater / license checker pings.
define( SELLCODES_STORE_URL, 'https://sellcodes.com/api/v2/licenses' ); // IMPORTANT: change the name of this constant to something unique to prevent conflicts with other plugins using this system
// OFFER ID of your offer Ex: https://sellcodes.com/jJPUX40f
define( SELLCODES_OFFER_ID, 'jJPUX40f' ); // IMPORTANT: change the name of this constant to something unique to prevent conflicts with other plugins using this system
Step 2 - Include the updater class
Please download this sample WordPress plugin, extract the sellcodes_plugin_updater.php & copy it into your own plugin's folder. After that change the sellcodes_sample_license_key" to your key that you are using to store the license key. We used key "sellcodes_sample_license_key" in our sample plugin for demonstration purposes.
Either way you simply need to refer to it properly in the code below, for example:
if( !class_exists( 'Sellcodes_Plugin_Updater' ) ) {
// load our custom updater
include( dirname( __FILE__ ) . '/sellcodes_plugin_updater.php' );
}
You may need to adjust the file path, depending on where you have decided to place the file within your plugin's folder structure.
Step 3 - Initiate the updater class
We now instantiate our updater class like this:
// retrieve our license key from the DB
$license_key = trim( get_option( 'sellcodes_sample_license_key' ) );
// setup the updater
$sellcodes_updater = new Sellcodes_Plugin_Updater( SELLCODES_STORE_URL, __FILE__, array(
'version' => '1.0', // current version number
'license' => $license_key, // license key (used get_option above to retrieve from DB)
'item_name' => SELLCODES_OFFER_ID, // offer id of this plugin
) );
The license key in this example is stored in an option called "sellcodes_sample_license_key". You will need to adjust this to retrieve the license key from the option you have set up for your plugin. If you need help setting up an option to store the license key in, take a look at the sample plugin as it includes a fully functional settings page.
The complete code for the updater will look something like this:
/*
Plugin Name: Sample Plugin
Plugin URI: http://sellcodes.com/
Description: Illustrates how to include an updater in your plugin for Sellcodes Licensing
Author: SlickCreator
Author URI: http://sellcodes.com/
Version: 1.0
*/
// this is the URL our updater / license checker pings.
define( SELLCODES_STORE_URL, 'https://api.sellcodes.com/v2/licenses' );
define( 'SELLCODES_PLUGIN_LICENSE_PAGE', 'sellcodes-license' );
// the offer id of your offer on Sellcodes
define( SELLCODES_OFFER_ID, “jJPUX40f” );
if( !class_exists( Sellcodes_Plugin_Updater ) ) {
// load our custom updater
include( dirname( __FILE__ ) . '/Sellcodes_Plugin_Updater.php' );
}
// retrieve our license key from the DB
$license_key = trim( get_option( sellcodes_sample_license_key ) );
// setup the updater
$sellcodes_updater = new Sellcodes_Plugin_updater( SELLCODES_STORE_URL, __FILE__, array(
'version' => '1.0', // current version number
'license' => $license_key, // license key (used get_option above to retrieve from DB)
'item_name' => SELLCODES_OFFER_ID)
);
Step 4 - Create the settings page
In order for a plugin to receive one-click updates, the license key needs to be activated. To activate a license key the customer will need to enter the key an a field within your plugin settings and then that key needs to be sent to the Sellcodes Licensing API.
Our sample plugin uses a simple settings page with a single input field. This can work quite well but is meant primarily for demonstrative purposes. It is recommended that you integrate the license key field on your existing settings page.
To create a simple settings page have a look at this code which creates a sub-menu under the Plugins menu called "Plugin License":
The idea here is that we first enter a license key and click "Save Changes", which causes the license key to be stored in our plugin/theme options. Once the option is stored, we click the "Activate License" button to trigger the API call.
The activate button is just a simple input field with a type of "submit" and a name attribute that is different than our save button. The names must be different so that we can know when the activate license button was clicked.
Step 5 - Activate the license key
To activate a license key, we "listen" for the Activate License button to be clicked then grab the value entered in the license key field and send an activation request to the
<?php
function sellcodes_sample_activate_license()
{
// listen for our activate button to be clicked
if( isset( $_POST['sellcodes_license_activate'] ) && !empty($_POST['sellcodes_sample_license_key']) )
{
update_option( 'sellcodes_sample_license_key', trim($_POST['sellcodes_sample_license_key']) );
// run a quick security check
if( ! check_admin_referer( 'sellcodes_sample_nonce', 'sellcodes_sample_nonce' ) )
return; // get out if we didn't click the Activate button
// retrieve the license from the database
$license = trim( get_option( 'sellcodes_sample_license_key') );
// data to send in our API request
$api_params = array(
'product_id' => SELLCODES_OFFER_ID,
'license_key' => $license,
'baseurl' => home_url()
);
// Call the API.
$response = wp_remote_post( SELLCODES_STORE_URL."/activate_license", array( 'timeout' => 15, 'sslverify' => false, 'body' => $api_params ) );
// make sure the response came back okay
$httpCode = wp_remote_retrieve_response_code( $response );
if ( 200 !== $httpCode) {
switch ($httpCode) {
case 500:case 0;
$message = __( 'An error occurred, please try again.' );
break;
case 400:
$license_data = json_decode(str_replace("\xEF\xBB\xBF",'',wp_remote_retrieve_body($response)));
if ( false === $license_data->success){
$message = $license_data->message;
}
break;
}
}
else
{
$license_data = json_decode(str_replace("\xEF\xBB\xBF",'',wp_remote_retrieve_body($response)));
if ( false === $license_data->success){
$message = $license_data->message;
}
else{
if(isset($license_data->License_key_valid) && false === $license_data->License_key_valid){
$message = __( 'Your license key has been disabled.' );
}
if(isset($license_data->Activation_ok) && false === $license_data->Activation_ok){
if($license_data->Activation_count == $license_data->Maximum_activations){
$message = __( 'Your license key has reached its activation limit.' );
}
}
}
}
// Check if anything passed on a message constituting a failure
$base_url = admin_url( 'plugins.php?page=' . 'SELLCODES_PLUGIN_LICENSE_PAGE' );
if (!empty( $message ))
{
$redirect = add_query_arg( array( 'sl_activation' => 'false', 'message' => urlencode( $message ) ), $base_url );
wp_redirect( $redirect );
exit();
}
$redirect = add_query_arg( array( 'sl_activation' => 'true', 'message' => 'true'), $base_url );
update_option( 'sellcodes_sample_license_status’, 'valid' );
wp_redirect( $redirect );
exit();
}
}
add_action('admin_init', 'sellcodes_sample_activate_license');
?>
Note: this is purely a sample and may look slightly different in your own implementation.
If everything runs okay after clicking the "Activate License" button, the activate button will be replaced with the word "active", and the license status will reflect the newly activated state in your Sellcodes’s store. If there is an error when activating the license key, the page will be reloaded and an error and message parameter will be added to the URL. We can then use the admin_notices hook (or any other applicable method) to display the error to the customer.
You can find the codes here.
Uploading new plugin versions
After you implemented the above, you can manage it directly on Sellcodes. Here is the full process:
1. Launch an offer https://sellcodes.com/launch-offer/about, set the license duration, and select it's a WordPress plugin
2. Upload the plugin zip file in the codes-section, as well as the readme.txt as separate attachment
3. Go to "Sales" => "License keys" => "General settings" and select the option "I want to offer automatic updates". Enter the current version of your plugin, and select from the drop-downs the zip file and readme file you want to use (the files you uploaded under step 2 will appear here in the drop downs). You don't need to enter anything in the Changelog, that will be pulled automatically from your readme.
4. After people bought your product, and you want to release an update, simply upload the new files (plugin zip file & readme) to your offer, go to the "I want to offer automatic updates"-section again and update it with the new information - Done!
That's it! If you have any questions about it, we're happy to help you with the integration. Simply reach out to us. Thank you!
Tell people about Sellcodes and get 20-40% on every resulting sale!