How To Add Licenses To Your WordPress Plugins

A simple step-by-step guide to help you easily add license key generation, activation and validation to your WordPress plugins.

Start using KeyKit
WordPress
KeyKit provides developers with the ability to manage the activation and usage of their software by creating and managing licenses. Adding product licensing to your WordPress plugins can unlock a number of exciting new possibilities. Maybe you want to offer advanced support for your plugin, provide automatic updates to customers, or even crack down on unauthorized use. Using product licensing can help simplify offering these features to your users.
With KeyKit, we want to make this process as simple as possible. Using our apis, you can easily generate license keys for your products, give them to your customers, and allow them to activate them when using your plugin. In this article, we will outline how you can quickly set this up for your own WordPress plugin using KeyKit.
A Quick Overview

At a high level, a basic example of how you can use KeyKit looks like this:
  • 1. A customer downloads or purchases your product.
  • 2. Using KeyKit's API, you generate a new license key for the product and give that key to the customer.
  • 3. The customer installs your plugin on their own site.
  • 4. Somewhere in your plugin, you include a form where the user can enter the license key in order to activate it.
  • 5. Once the license key is submitted, your plugin on the customer’s site will make a request to KeyKit to verify that the license is valid. KeyKit will check the submitted key along with the domain registering it. At this point we will run through a series of checks on our end based off of both rules we have in place as well as custom rules you can set up for your products and licenses.
  • 6. If all the checks pass and the submitted license key is valid, KeyKit will return a response to inform your plugin that the license has successfully been activated.
  • 7. Once your plugin on the customer's site receives a successful response from KeyKit, your plugin knows that the customer has entered a valid license key. You can now save the license key and associated information and grant full access of your plugin to the user.
  • 8. In a case where the activation failed (e.g. invalid key, expired key, blocked key, etc), we will return the appropriate response to your plugin so that you can inform the customer that the activation has failed.
  • 9. Using our /licenses/check endpoint, you can periodically check the license's status and take action if anything changes (for example if a license expires).
  • 10. That's it!
Step 1: Create a New Api Key

Creating a new Api key in KeyKit is as simple as clicking a button:

Your Api Key will give you the ability to create new products and licenses through the KeyKit Api. Your Api Key is private and should not be shared or placed into any client facing code at any point. We will look at more examples of using your api key in just a moment.
Step 2: Create A New Product

In KeyKit, licenses are tied to "Products". You'll first need to create a new Product in your account that represents the product you intend to distribute and create license for. This can be created either from within your KeyKit account or via the api using the endpoint https://keykit.io/api/v1/products
Here is an example of creating a new product from your dashboard:

Note: When you create a new product, a unique product id (product_ref) will be automatically generated for you. Hang onto this, as we will use it in a moment.

Product Reference Key
Step 3: Creating Licenses

Now that you have an api key and a product, creating licenses is easy. Let's go back the example outlined above for a second. When a customer downloads or buys your product, you can now generate a new license for them from your own server using the KeyKit Api.
Here is an example of creating a new license for our product using Insomnia to make the api request. The only required values we need in order to do this are our api key which we created in step one, and the product reference key (product_ref), which was automatically generated for us when we created a new product in step 2. In order to create a license now, we simply need to make a post request to https://keykit.io/api/v1/licenses like so:

The example above demonstates how you can create a new license through the KeyKit api using a web request tool like Insomnia or Postman and your KeyKit api token. However, in practice, what you'll actually want to do is create the api request from your own code on your own server to generate the key after a user successfully purchases or downloads your product. Let's take a look at how this can be done with an example just using php to make the request to KeyKit:

  // === Generate a new License Key For a Product ===
  // === Request: ===

  <?php

  $url = 'https://keykit.io/api/v1/licenses';

  $ch = curl_init($url);
  
  $token = YOUR_KEYKIT_API_TOKEN; 
  $fields = json_encode(array(
    'product_ref'=> 
    'prod_118830f35b2c0779ccac'
  ));
  
  $authorization = "Authorization: Bearer ".$token;
  curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json', 
    $authorization));

  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

  $result = curl_exec($ch);
  curl_close($ch);

  

In this example, we will get back a successful response from KeyKit with the new license we have created for our customer:



  # === Response: ===
  
  echo $result;  
  
  {
    "data": {
      "id": "25",
      "type": "license_key",
      "attributes": {
        "key": "a9e48217-203c-4c8a-b5cb-933fd4c95e06",
        "status": "pending",
        "activated_at": null,
        "expired_at": null,
        "expired": false
      }
    }
  }
  
  // Get The License Key:
  echo $result["data"]["attributes"]["key"];
  "a9e48217-203c-4c8a-b5cb-933fd4c95e06"


Just like that, a new license has been created, and the key can be shared with your customer downloading the product. At this point, the key is not yet activated yet however, as this step will be performed from within your plugin when the user goes on to install and activate it.
Step 4: Activating Licenses In Your Plugins

Now that we have covered how to get an api key, create a product, and start creating licenses for your product, the last step is license activation.
To begin, in your plugin, you'll want to define a constant that sets your product reference key. This should match the Product Ref in your dashboard for the product you want to create licenses for.

  <?php

    // This is your KeyKit Product Ref, used for license key activation/validation.
    // This should match a Product Ref in your dashboard for the product you want to create licenses for:
    define( 'KEYKIT_PRODUCT_REF', 'prod_118830f35b2c0779ccac' );

  

Next, you'll want to add a form to your plugin where your customer can submit their license key to activate and unlock the plugin. Below is a basic example of how you can take the submitted license key, the domain the plugin is being activated on, and your unique product id (product_ref), and make the request to keykit to validate a license key:



    // Get the domain for the site activating the license key.
    $domain = wp_parse_url(get_bloginfo('url'));

    // Get the license key being submitted.
    $license_key = sanitize_text_field($_POST['license_key']);  

    // Set the product this license key is being activated for
    $product_ref = KEYKIT_PRODUCT_REF;

    // Set the KeyKit Api url
    $api_url = 'https://keykit.io/licenses/activate';

    $attributes = array(
        'key' => $license_key,
        'product_ref' => $product_ref,
        'domain'      => $domain['host']
    );

    // Make the request to KeyKit and get the response   
    $request = wp_remote_post($api_url, $attributes);
    $response = json_decode(wp_remote_retrieve_body($request));
    
    // Check if the activation was successfully       
    if ($response['valid']) {
        // Success! The license key was successfully activated
        
        //Save the license key
        update_option('keykit_license_key', $response->license_key);

        // Activate your plugin!


    } else {
        
        // The license key activation was unsuccessful.
        
        echo $response;
    }


Additionally, once a license key is active, you can now periodically check it as you need to ensure a key is valid at various points. This will come in handy if, for example, you set up your licenses up to expire at a future date.


    // Get the domain.
    $domain = wp_parse_url(get_bloginfo('url'));

    // Get the license key.
    $license_key = get_option('keykit_license_key');  

    // Get the Product Reference Key
    $product_ref = KEYKIT_PRODUCT_REF;

    // Set the KeyKit Api url
    $api_url = 'https://keykit.io/licenses/check';

    $attributes = array(
        'key' => $license_key,
        'product_ref' => $product_ref,
        'domain'      => $domain['host']
    );

    // Make the request to KeyKit and get the response   
    $request = wp_remote_post($api_url, $attributes);
    $response = json_decode(wp_remote_retrieve_body($request));
    
    // Check if the license key is valid       
    if ($response['valid']) {
        // Success! The license key is valid
        
        // Do something!

    } else {
        
        // The license key is NOT valid.

        // Do something else!

        echo $response;
    }

Next Step: View Our Api Docs

View Api Docs