Categories
Advanced Tutorials

Add License Key to Premium Theme/Plugin (3/3)

How to use your API server and glue everything we have done so far together.

I showed you how to set up your API server in parts one and two. Up until this point, you must have it working and fully tested.

Many found our in-depth article on Recommended Tools and Software to be helpful as well.

In the final part of this series, I will show you how to integrate everything we have done so far and glue it together.

Note: The following code must be included within your premium theme or plugin.

So, let’s get started.

The Setup

Before we move forward again, we will use Composer to load up a couple of packages for our HTTP client. This will help us make requests from the plugin site to the API server.

To do that, let us create the composer.json file and run the “composer update” command from the terminal inside the directory with this file. This will basically download all the required packages and create the dependencies inside the vendor folder.

{
  "require": {
    "guzzlehttp/guzzle": "^6.0"
  }
}

Note: Guzzle HTTP is the client we will use to send requests and receive data from our API server.

The Structure

Once you run the composer update, you will have the following directory structure inside your plugin API directory.

plugin-name/
  form.php
  api/
    vendor/
      composer.json
      composer.lock
      index.php

Note: vendor and composer. A lock is generated when you run the composer update command.

The Form

Before I show you the code which will link the plugin with the API server execute the authentication, activation, and deactivation of the license key (for the current domain name), let me show you the HTML form.

I created this sample form for this tutorial, but you can easily load/integrate it any way you wish within your premium plugin or a theme.

Explore this highly recommended read on Overview of Initial Development and Outsourcing Costs

It’s a two-step process wherein, in step one, the user needs to authenticate with the previously created username and password. Then, they will be able to Activate or Deactivate the plugin for the current domain name.

Notices there is a switch we add or remove locally called license_key_is_confirmed based on which you will show any premium features within your plugin or a theme.

The Code

The last step of the whole process is to link the form above with the API server and glue everything together.

The code is pretty much self-explanatory.

Many found our in-depth article on The Power of the Search and Replace Pro for Elementor to be helpful as well.

Once we send the request to our API server, we get the license key and available list with active sites. Based on this, we can either let the user activate/deactivate the current site or display an error message (e.g., if user credentials are incorrect or if the limit of total available installs has been reached).

Additionally, we use try … catch to display some server errors, which can help your users debugging or bug reporting.

The Repo

I am currently creating a GitHub repository. You can download a fully working example based on the series and, with minimal changes, have a working API server with a license key feature for your premium plugin or a theme.

I really hope this was helpful and useful and you didn’t have any problems following the process. Don’t hesitate to ask any questions in the comments below.

‘Til the next one.

For more on Reviews, don't forget to explore our resource on Elevate Your WordPress Block Editor with Animate Blocks on Scroll

Leave a Reply

Your email address will not be published. Required fields are marked *