Checking if License Keys are Valid in WordPress Plugins and Themes

You can easily check if a license key is valid at any time. You may want to do this in order to limit certain functionality in the theme or plugin to only users with a valid license key.

In this documentation, I’m going to show you how to check if a license is valid 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.

We are going to assume that you have already activated your license key, but if you haven’t, or if you need integration instructions, read this doc.

Checking a license key’s validity is quite simple; all it requires is that we perform a remote request to our store website with a couple of specific parameters. See the function below:

/************************************
* this illustrates how to check if  a license key is still valid
* the updater does this for you, so this is only needed if you
* want to do something custom
*************************************/

function wpls_sample_check_license() {

    global $wp_version;

    $license = get_option( 'wpls_sample_license_key' );
        
    $api_params = array(
        'wpls_action' => 'check_license',
        'license' => $license,
        'code' => urlencode( WPLS_SAMPLE_ITEM_CODE ),
        '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 ) );

    if ( is_wp_error( $response ) )
        return false;

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

So you just need check license status by call wpls_sample_check_license()


$status = wpls_sample_check_license();

if($status->license_status == 'valid') {
   // pass value
}
else {
   // invalid value
}

Recent Post