Creating a Plugin Activation Licensing System

Posted on 18th June 2023

Introduction

In this tutorial, we will be creating a plugin activation licensing system for a WordPress plugin. This system will allow for the plugin to be activated on a single site or domain. Once the plugin has been activated, it will be able to be used on that site or domain for an unlimited amount of time. If the plugin is deactivated, it will no longer be able to be used on that site or domain.

We will be using the WordPress Settings API to create our plugin options page and to save our plugin activation license key. We will also be using the WordPress Transients API to store our plugin activation license key in the database.

Creating the Plugin Options Page

The first thing we need to do is create the plugin options page. This page will be where the user will enter their activation license key. We will be using the WordPress Settings API to create our plugin options page.

First, we need to add the following code to our plugin file:

function my_plugin_options_page() {

?>

My Plugin Options Page

<?php

}

This code will create the plugin options page and will display a form. The form will post to the options.php file, which will save our plugin options.

Next, we need to register our settings. We will do this by adding the following code to our plugin file:

function my_plugin_register_settings() {

register_setting( 'my_plugin_options', 'my_plugin_options', 'my_plugin_options_validate' );

add_settings_section( 'my_plugin_main', 'Main Settings', 'my_plugin_section_text', 'my_plugin_options' );

add_settings_field( 'my_plugin_license_key', 'License Key', 'my_plugin_setting_license_key', 'my_plugin_options', 'my_plugin_main' );

}

add_action( 'admin_init', 'my_plugin_register_settings' );

This code will register our plugin settings and will add a settings section and a settings field for our activation license key.

Next, we need to add the following code to our plugin file:

function my_plugin_section_text() {

echo '

Enter your license key to activate your plugin.

';

}

This code will add the text to our settings section.

Next, we need to add the following code to our plugin file:

function my_plugin_setting_license_key() {

$options = get_option( 'my_plugin_options' );

echo '';

}

This code will add the input field for our activation license key.

Next, we need to add the following code to our plugin file:

function my_plugin_options_validate( $input ) {

$newinput['license_key'] = trim( $input['license_key'] );

if( ! preg_match( '/^([a-z0-9]+)$/i', $newinput['license_key'] ) ) {

$newinput['license_key'] = '';

}

return $newinput;

}

This code will validate our activation license key. The license key must be alphanumeric.

Next, we need to add the following code to our plugin file:

function my_plugin_menu() {

add_options_page( 'My Plugin Options Page', 'My Plugin', 'manage_options', 'my-unique-identifier', 'my_plugin_options_page' );

}

add_action( 'admin_menu', 'my_plugin_menu' );

This code will add the plugin options page to the WordPress admin menu.

Saving the Plugin Activation License Key

Once the user has entered their activation license key and clicked the “Save Changes” button, we need to save the license key in the database. We will be using the WordPress Settings API to save our plugin options.

First, we need to add the following code to our plugin file:

function my_plugin_options_update() {

$options = get_option( 'my_plugin_options' );

$options['license_key'] = trim( $_POST['my_plugin_options']['license_key'] );

update_option( 'my_plugin_options', $options );

}

add_action( 'admin_post_update', 'my_plugin_options_update' );

This code will save our plugin options when the user clicks the “Save Changes” button.

Checking the Plugin Activation License Key

Now that we have saved the plugin activation license key in the database, we need to check it against our activation server. We will do this by making an HTTP request to our activation server.

First, we need to add the following code to our plugin file:

function my_plugin_activate() {

$options = get_option( 'my_plugin_options' );

$license_key = $options['license_key'];

$request_url = 'http://example.com/?license_key=' . $license_key;

$response = wp_remote_get( $request_url );

if( is_array( $response ) ) {

$body = $response['body'];

if( $body == 'valid' ) {

$options['is_activated'] = '1';

update_option( 'my_plugin_options', $options );

}

}

}

register_activation_hook( __FILE__, 'my_plugin_activate' );

This code will check the plugin activation license key against our activation server. If the license key is valid, it will update the “is_activated” option in the database.

Deactivating the Plugin

If the plugin is deactivated, we need to remove the license key from the database. We will do this by using the WordPress Settings API.

First, we need to add the following code to our plugin file:

function my_plugin_deactivate() {

$options = get_option( 'my_plugin_options' );

$options['license_key'] = '';

update_option( 'my_plugin_options', $options );

}

register_deactivation_hook( __FILE__, 'my_plugin_deactivate' );

This code will remove the license key from the database when the plugin is deactivated.

Uninstalling the Plugin

If the plugin is uninstalled, we need to remove the plugin options from the database. We will do this by using the WordPress Settings API.

First, we need to add the following code to our plugin file:

function my_plugin_uninstall() {

delete_option( 'my_plugin_options' );

}

register_uninstall_hook( __FILE__, 'my_plugin_uninstall' );