Activating License Keys in WP Plugins and Themes

In order for a license key to be fully utilized, it must be activated. This can happen in one of two ways:

  1. A site admin can manually click the “Activate” link for the license from the Licensing System > License Dashboard page
  2. The buyer can remotely activate the license via a system in your plugin/theme/software that uses the API to trigger the activation

In this documentation, I’m going to show you how to activate a license remotely via a system added to your WordPress theme or plugin. The sample code I’m showing you in this example is the same exact code that is included with the sample plugin and themes, available for download after purchasing WordPress Licensing System.

There are two main components to activating a license remotely:

  1. Saving the license key in the data (a theme or plugin option)
  2. Sending the stored license key through the API to the store website for verification and activation

When a license is remotely activated, the status of the license in your site’s dashboard will be updated from “inactive” (the default state) to “active”.

Let’s first look at creating a simple option’s page to store our license.

/************************************
* the code below is just a standard
* options page. Substitute with
* your own.
*************************************/

function wpls_sample_license_menu() {
        add_plugins_page( 'Licensed Plugin', 'Licensed Plugin', 'manage_options', 'licensed-plugin', 'wpls_sample_license_page' );
}
add_action('admin_menu', 'wpls_sample_license_menu');

function wpls_sample_license_page() {
        $license        = get_option( 'wpls_sample_license_key' );
        $status         = get_option( 'wpls_sample_license_status' );
        $type           = get_option( 'wpls_sample_license_key_type' );
        ?>
        <div class="wrap">
                <h2><?php _e('Licensed Plugin Options'); ?></h2>
                <form method="post" action="options.php">

                        <?php settings_fields('wpls_sample_license'); ?>

                        <table class="form-table">
                                <tbody>
                                        <tr valign="top">
                                                <th scope="row" valign="top">
                                                        <label for="wpls_sample_license_url"><?php _e('Where you get license?'); ?></label>
                                                </th>
                        <td>
                                <?php _e('Please visit <a href="http://yourdomain.com">http://yourdomain.com</a> to get your license for free.'); ?>
                        </td>
                                        </tr>
                                        <tr valign="top">
                        <th scope="row" valign="top">
                                                        <label for="wpls_sample_license_key"><?php _e('License Key:'); ?></label>
                                                </th>
                                                <td>
                                                        <input id="wpls_sample_license_key" name="wpls_sample_license_key" type="text" class="regular-text" value="<?php esc_attr_e( $license ); ?>" />
                                                        <p class="description"><?php _e('Enter your license key'); ?></p>
                                                </td>
                                        </tr>
                                        <?php if( false !== $license ) { ?>
                                                <tr valign="top">
                                                        <th scope="row" valign="top">
                                                                <label for="act_button"><?php _e('Action Button:'); ?></label>
                                                        </th>
                                                        <td>
                                                                <?php if( $status !== false && ($status == 'active' || $status == 'activated') ) { ?>
                                                                        <?php wp_nonce_field( 'wpls_sample_nonce', 'wpls_sample_nonce' ); ?>
                                                                        <input type="submit" class="button-secondary" name="wpls_license_deactivate" value="<?php _e('Deactivate License'); ?>"/>
                                                                        <span style="color:green;"><?php _e('Active'); ?></span>
                                                                <?php } else {
                                                                        wp_nonce_field( 'wpls_sample_nonce', 'wpls_sample_nonce' ); ?>
                                                                        <input type="submit" class="button-secondary" name="wpls_license_activate" value="<?php _e('Activate License'); ?>"/>
                                                                <?php } ?>
                                                        </td>
                                                </tr>
                        <tr valign="top">
                        <th scope="row" valign="top"><?php _e('License data');?></th>
                        <td><?php echo wpls_sample_get_license_data();?></td>
                        </tr>
                        <tr valign="top">
                        <th scope="row" valign="top"><?php _e('Advance Feature');?></th>
                        <td>
                                                        <p><strong><?php _e('BASIC FEATURES');?></strong></p>
                            <p>
                                <ol>
                                        <li><?php _e('FEATURE #1');?></li>
                                        <li><?php _e('FEATURE #2');?></li>
                                        <li><?php _e('FEATURE #3');?></li>
                                </ol>
                            </p>
                                                        <p><strong><?php _e('MORE FEATURES?');?></strong></p>
                                                        <?php if($type == 'PRO' || $type == 'DEVELOPER'){?>
                            <p>
                                <ol>
                                        <li><?php _e('FEATURE #4');?></li>
                                        <li><?php _e('FEATURE #5');?></li>
                                        <li><?php _e('FEATURE #6');?></li>
                                </ol>
                            </p>
                                                        <?php
                                                        }
                                                        else {
                                                                echo '<p><strong><a href="'.wpls_sample_ugeturl('PRO').'">'.__('Upgrade to more...').'</a></strong></p>';
                                                        }
                                                        if($type == 'DEVELOPER'){?>
                            <p>
                                <ol>
                                        <li><?php _e('FEATURE #7');?></li>
                                        <li><?php _e('FEATURE #8');?></li>
                                        <li><?php _e('FEATURE #9');?></li>
                                </ol>
                            </p>
                            <?php
                                                        }
                                                        else {
                                                                echo '<p><strong><a href="'.wpls_sample_ugeturl('DEVELOPER').'">'.__('Upgrade to more...').'</a></strong></p>';
                                                        }
                                                        ?>
                        </td>
                        </tr>
                                        <?php } ?>
                                </tbody>
                        </table>
                        <?php submit_button(); ?>

                </form>
        <?php
}

function wpls_sample_register_option() {
        // creates our settings in the options table
        register_setting('wpls_sample_license', 'wpls_sample_license_key', 'wpls_sanitize_license' );
}
add_action('admin_init', 'wpls_sample_register_option');

function wpls_sanitize_license( $new ) {
        $old = get_option( 'wpls_sample_license_key' );
        if( $old && $old != $new ) {
                delete_option( 'wpls_sample_license_status' ); // new license has been entered, so must reactivate
        }
        return $new;
}

This code sets up submenu to the Plugins menu called “Licensed Plugin”. I’m using a plugins page in this example, but the code is identical for theme’s.

There are three lines at the top of the code:

$license    = get_option( 'wpls_sample_license_key' ); // define license key
$status     = get_option( 'wpls_sample_license_status' ); // define license status
$type       = get_option( 'wpls_sample_license_key_type' ); // define license type

The first is the license itself and the second is the status of the license. Once we have activated our license, we will change the status (on our local site) to “active”. This is so that we can show an “Activate License” button if the license has not yet been activated, and hide the button if it has. See the screenshot below:

wpls-sample

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.

In order to activate the license, we just need to “watch” for when the activate button is clicked. The way that we do this is by setting up a function that is hooked to the “admin_init” hook, like so:

/************************************
* this illustrates how to activate
* a license key
*************************************/

function wpls_sample_activate_license() {

    // listen for our activate button to be clicked
    if( isset( $_POST['wpls_license_activate'] ) ) {

        // run a quick security check
         if( ! check_admin_referer( 'wpls_sample_nonce', 'wpls_sample_nonce' ) )     
            return; // get out if we didn't click the Activate button

        // retrieve the license from the database
        $license = get_option( 'wpls_sample_license_key' );    

        // data to send in our API request
        $api_params = array(
            'wpls_action' => 'activate',
            'license' => $license,
            'code' => urlencode( WPLS_SAMPLE_ITEM_CODE ), // the CODE of our product in WPLS
            'url' => home_url(),
            'host' => isset($_SERVER['SERVER_ADDR']) ? $_SERVER['SERVER_ADDR'] : $_SERVER['LOCAL_ADDR']
        );
        
        // Call the custom API.
        $response = wp_remote_get( add_query_arg( $api_params, WPLS_SAMPLE_STORE_URL ), array( 'timeout' => 15, 'sslverify' => false ) );

        // make sure the response came back okay
        if ( is_wp_error( $response ) )
            return false;

        // decode the license data
        $license_data = json_decode( wp_remote_retrieve_body( $response ) );
        
        // $license_data->license_status will be either "valid" or "invalid"

        update_option( 'wpls_sample_license_status', $license_data->license_status );
        update_option( 'wpls_sample_license_key_type', $license_data->license_type );

    }
}

add_action('admin_init', 'wpls_sample_activate_license');

You can parse upgrade url by adding remote callback to WPLS server, like:

function wpls_sample_ugeturl($type) {

        $license = trim( get_option( 'wpls_sample_license_key' ) );

        $api_params = array(
                'wpls_action' => 'do_upgrade',
                'license' => $license,
                'code' => urlencode( WPLS_SAMPLE_ITEM_CODE ), // the CODE of our product in WPLS
                'url' => home_url(),
                'host' => isset($_SERVER['SERVER_ADDR']) ? $_SERVER['SERVER_ADDR'] : $_SERVER['LOCAL_ADDR'],
                'type' => $type
        );

        $response = wp_remote_get( add_query_arg( $api_params, WPLS_SAMPLE_STORE_URL ), array( 'timeout' => 15, 'sslverify' => false ) );

        if ( is_wp_error( $response ) )
                return false;

        $license_data = json_decode( wp_remote_retrieve_body( $response ) );

        return ($license_data->upgrade_url == false ? '#' : $license_data->upgrade_url);
}

so you just need to call that function like:

<a href="<?php echo wpls_sample_ugeturl('PRO');?>">Upgrade to Pro version</a>

that above code you must see <?php echo wpls_sample_ugeturl('PRO');?> that will parse upgrade url from WPLS server. So when user click that Url, user will auto redirect to payment url.

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 WordPress Licensing System dashboard.

Once the license is activated, the customer can receive automatic upgrade notices.

Recent Post