Software Licensing - Checking if License Keys are Valid in WordPress Plugins and Themes

Software Licensing is an extension for Easy Digital Downloads that makes it easy to license your digital products. Learn more at the main Easy Digital Downloads website.

With  Software Licensing 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, we'll show how to check if a license is valid remotely via a system added to your WordPress theme or plugin. The sample code shown in this example is the same exact code that is included with the sample plugin and themes, available for download after purchasing the  add-on.

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:

function edd_sample_theme_check_license() {
	$store_url = 'http://yoursite.com';
	$item_name = 'Your Item Name';
	$license = '834bbb2d27c02eb1ac11f4ce6ffa20bb';
	$api_params = array(
		'edd_action' => 'check_license',
		'license' => $license,
		'item_name' => urlencode( $item_name ),
		'url' => home_url()
	);
	$response = wp_remote_post( $store_url, array( 'body' => $api_params, 'timeout' => 15, 'sslverify' => false ) );
  	if ( is_wp_error( $response ) ) {
		return false;
  	}

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

	if( $license_data->license == 'valid' ) {
		echo 'valid';
		exit;
		// this license is still valid
	} else {
		echo 'invalid';
		exit;
		// this license is no longer valid
	}
}

Note: if you are consistently checking the validity of a license key, perhaps to block usage of the plugin or theme when the license expires, you MUST cache the results of the check so that the request is not performed with every page load. The Transients API is a great way to cache API responses.