Adding License Key Feature to Your Premium WordPress Theme or Plugin (3/3)

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



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

In the final part of these series, I will show you how you can integrate everything we did 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 are going to use Composer to load up a couple of packages for our HTTP client. This will help us to 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 basically will 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 are going to use to send requests and received data from our API server.

Subscribe to our Newsletter* if you find our content useful and would like to receive regular updates with the latest posts.

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.lock are 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.

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

It’s a two-step process wherein step one the user needs to authenticate with the previously created username and password and 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.

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 with debugging or bug reporting by your users.

The Repo

I am currently creating a GitHub repository from where you can download a fully working example based on the series and with minimal changes have a working API server with 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 to follow the process. Don’t hesitate to ask any questions in the comments below.

‘Til the next one.

* We have a NO SPAM policy so you won't reaceive any meaningless emails from us!

Related Content

Adding License Key Feature to Your Premium WordPress Theme or Plugin (2/3)

How to set up our API server and link up the previously created functions with our server app.

READ MORE...

Create Gutenberg Blocks with ACF 5.8+ PRO

How to register and customize Gutenberg blocks with the Advanced Custom Fields plugin (no JavaScript needed)

READ MORE...

Overwrite and Send Custom Emails in WordPress

How to send custom emails using wp_mail() and overwrite all WordPress registration emails.

READ MORE...

Leave a Reply

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

Track your WordPress.org Plugin Stats on Your Site
Adding License Key Feature to Your Premium WordPress Theme or Plugin (3/3)

Thank you for reading our content. Use the buttons below to spread the love and share Overwrite and Send Custom Emails in WordPress with your followers.