Creating a Plugin Activation License System

Posted on 16th June 2023

Introduction

In this article, we will be discussing how to create a plugin activation license system for your WordPress plugin. A plugin activation license system is a great way to ensure that your plugin is only used by those who have purchased it. This can help to reduce piracy of your plugin, and it can also help to ensure that only those who are willing to pay for your plugin are using it.

Creating a Plugin Activation License System

There are two parts to creating a plugin activation license system:

  1. The first part is creating a unique license key for each purchase of your plugin. This license key will be used to activate the plugin on a WordPress site.
  2. The second part is creating a system that will check if a plugin is activated on a WordPress site using the license key. If the plugin is not activated, then it will not be able to be used on that WordPress site.

Part 1: Creating a Unique License Key for Each Purchase

The first part of creating a plugin activation license system is creating a unique license key for each purchase of your plugin. To do this, you will need to generate a license key using a service such as WP License Keys.

Part 2: Creating a System That Checks If a Plugin Is Activated

The second part of creating a plugin activation license system is creating a system that will check if a plugin is activated on a WordPress site using the license key. To do this, you will need to create a custom function that will check if a license key is valid. This custom function will need to be added to your plugin code.

Conclusion

In this article, we have discussed how to create a plugin activation license system for your WordPress plugin. This system can help to reduce piracy of your plugin and it can also help to ensure that only those who are willing to pay for your plugin are using it.

If you’re looking to create a plugin activation license system for your WordPress site, there are a few different ways you can go about it. In this article, we’ll cover two different methods: using a third-party service, and creating your own system.

Using a Third-Party Service

There are a few different services out there that will allow you to create a license system for your WordPress plugins. One of the most popular is WP License Manager.

With WP License Manager, you can create license keys for your plugins and then distribute them to your customers. The service also allows you to track which keys have been used and when they were used.

Another popular service is MyPluginLicense.com. MyPluginLicense.com offers a similar service to WP License Manager, but with a few additional features.

With MyPluginLicense.com, you can create “time-limited” license keys, which will expire after a certain amount of time. You can also add “custom fields” to your license keys, which allows you to track additional information about your customers.

Creating Your Own System

If you don’t want to use a third-party service, you can also create your own plugin activation license system. This method is a bit more complex, but it has the advantage of being completely under your control.

The first thing you’ll need to do is create a custom database table to store your license keys. You can use any database management system you want, but we recommend using MySQL.

Once you have your database table set up, you’ll need to create a PHP script to generate license keys. This script should be able to generate keys of any length and with any character set you want.

Once you have your script set up, you’ll need to create a form on your WordPress site where users can input their license key. This form should then save the license key to the database table you created earlier.

Finally, you’ll need to create a “verification” script that will check if a license key is valid. This script should be able to check if a license key exists in the database, and if it does, it should return the information associated with that key.

If you follow these steps, you’ll have a fully functioning plugin activation license system set up on your WordPress site.

The next step is to add the code that will actually validate the license key. In your plugin, create a new file called license.php and add the following code.

file = $file;
$this->item_name = $item_name;
$this->item_shortname = ‘myplugin’;
$this->version = $version;
$this->author = $author;

// set license key
$this->license = trim( get_option( $this->item_shortname . ‘_license_key’ ) );

// set api url
if ( ! empty( $_SERVER[‘SERVER_NAME’] ) ) {

$domain = $_SERVER[‘SERVER_NAME’];

} else {

$domain = ‘localhost’;

}

$this->api_url = str_replace( ‘DOMAIN’, $domain, $this->api_url );

}

// license key check
public function license_key_check() {

$params = array(
‘license’ => $this->license,
‘item_name’ => urlencode( $this->item_name ),
‘domain’ => urlencode( $_SERVER[‘SERVER_NAME’] ),
‘instance’ => urlencode( home_url() )
);

// send request
$response = wp_remote_get( add_query_arg( $params, $this->api_url ), array( ‘timeout’ => 20, ‘sslverify’ => false ) );

// check for errors
if ( is_wp_error( $response ) )
return false;

// decode license data
$license_data = json_decode( wp_remote_retrieve_body( $response ) );

// exit if response doesn’t have license data
if ( !is_object( $license_data ) )
return false;

// return license data
return $license_data;

}

// license key activation
public function license_activate() {

$license_data = $this->license_key_check();

// exit if license data isn’t returned or there was an error
if ( ! $license_data || $license_data->license == ‘invalid’ ) {
return false;
}

// get license type
$license_type = $license_data->license;

// return true if license is valid
if ( $license_type != ‘invalid’ ) {
return true;
}

// return false if license is invalid
return false;

}

// license key deactivation
public function license_deactivate() {

$params = array(
‘license’ => $this->license,
‘item_name’ => urlencode( $this->item_name ),
‘domain’ => urlencode( $_SERVER[‘SERVER_NAME’] ),
‘instance’ => urlencode( home_url() )
);

// send request
$response = wp_remote_get( add_query_arg( $params, $this->api_url ), array( ‘timeout’ => 20, ‘sslverify’ => false ) );

// check for errors
if ( is_wp_error( $response ) )
return false;

// decode license data
$license_data = json_decode( wp_remote_retrieve_body( $response ) );

// exit if response doesn’t have license data
if ( !is_object( $license_data ) )
return false;

// return license data
return true;

}

// license key check function
public function license_key_status() {

$license_data = $this->license_key_check();

// exit if license data isn’t returned or there was an error
if ( ! $license_data || $license_data->license == ‘invalid’ ) {
return false;
}

// get license type
$license_type = $license_data->license;

// return true if license is valid
if ( $license_type != ‘invalid’ ) {
return true;
}

// return false if license is invalid
return false;

}

// display notice if license key is missing
public function license_missing_notice() {

if ( isset( $_GET[‘page’] ) && $_GET[‘page’] == $this->item_shortname . ‘-license’ )
return;

if ( ! $this->license_key_status() ) {

echo ‘

‘ . sprintf( __( ‘A valid %1$s license key is required for %1$s to work. Please enter your license key here.’, $this->item_shortname ), $this->item_name, admin_url( ‘admin.php?page=’ . $this->item_shortname . ‘-license’ ) ) . ‘

‘;

}

}

}
}