We launched the multi-tenant WordPress Marketplace! 🎉

Automatically activating license keys for new tenants

Automatically activating license keys for new tenants

We all want to automate the activation of license keys for plugins, but first we have to talk about how license keys are stored in your WP install.

There are two places where license keys can be stored:

  • In the wp-config.php
  • In the database

Plugin license keys in the WP-config.

When you’re reading the instructions to activate the license key for plugin, it will be clear if your license key is stored in the WP-config.php.

How? Because the instructions will likely say something like “To activate your license via your wp-config.php file, add the following line to the file, replacing [key] with your license key:”

Plugin license keys in the WP-config.

When a license key is stored in the WP-config, you’ll generally need to define a ‘NAME’ and a ‘KEY’. In the example above, which is the ACF plugin, the ‘NAME’ is ACF_PRO_LICENSE and the ‘KEY’ is given to you personally.

So, how to insert this into WPCS?

PHP Constants in the Tenant Overview

Feature alert: PHP Constants in the Tenant Overview

For this, we have our feature PHP Constants. As you may know, WPCS does not allow access to the WP-config. Instead, you can add a PHP constant by navigating to your specific tenant and adding the license key. 

At this time, you can’t automatically insert license keys for new tenants for plugins that store the license key in the WP-config. You can only do that by manually defining them as a PHP constant in the tenant overview. Soon, we’ll make it possible to define PHP constants at the Version level as well. That will automatically insert these PHP constants for all tenants inside that Version. We’ll keep you updated on that in our FB community.

Defining a PHP Constant in WPCS

PHP constants can be used to inject secrets or configurations into your tenant. Please note that both Name and Value are case-sensitive.

Defined constants will be available in your tenant as actual PHP constants. As if the following PHP code was added to a plugin or theme’s functions.php file:

define('NAME', 'VALUE');

As mentioned in the example above, the ‘NAME’ is ACF_PRO_LICENSE and the ‘KEY’ is given to you personally.

One last thing: a sensitive constant cannot be read after adding and can only be completely overwritten.

As a reminder:

WPCS does not give access to the WP-config because we have set up the WP-config specifically to support multitenancy. Adjusting this could lead to a lot of things being broken, and that applies to all tenants (because multitenancy).

Plugin license keys in the database.

You’ll know if the license key is stored in the database if it doesn’t mention the WP-config and also usually when you can insert the license key directly from within the overview of the plugin. 


Plugin license keys in the database. ​

Feature alert: Lifecycle Hooks

Fortunately, you can automatically activate license keys in new tenants for plugins that store the license key in the database. We use our feature Lifecycle Hooks to trigger a PHP Snippet. In this case, we need to hook into a Tenant Lifecycle Hook.

To utilize the Tenant Lifecycle Hooks, you use the Code Snippets plugin. It’s easiest if you install this plugin with the PHP snippet that contains the license key and make a Snapshot before you create any tenants. Because the PHP snippet inside the plugin is stored in the database, the license key is always activated when you create new tenants.

Want to learn more?

If you’re interested in learning how to install and activate new plugins for existing tenants, sign up for one of our webinars showing you how to create a must-use plugin to activate new plugins and create your own plugin to trigger the license key.

Note:

New tenants are created from Snapshots and will represent a perfect clone of the tenant or version that it’s created from. This means that plugins that were activated in the version or tenant at the time of snapshot creation will also be activated in new tenants, as the configuration that activates a tenant is stored in the database.
When you combine this with the PHP snippet inside the Code Snippet plugin, it will instantly activate the license key upon tenant creation.

Whatever PHP is in the Code Snippets plugin, it will run upon tenant creation. So, if you launch new tenants and have inserted a PHP snippet in the Code Snippets plugin with the license key in the Snapshot you use, your license key will automatically be activated. 

How to get a PHP snippet to activate your plugin license

Let’s discuss an example of what a PHP snippet could look like. But before you write one yourself, we recommend an easier way to get the PHP snippet for your lifecycle hook.

Our recommendation if you want to create a lifecycle hook for activating licensenses:

Ask for a PHP snippet

Reach out to the support team of the plugin you want to use and ask them “how to automate license activation of the plugin with PHP”. They will provide you with a PHP snippet that you can use in the PHP Snippets plugin.

How to get a PHP snippet to activate your plugin license

For the purpose of this article, let’s have a quick look at licensing Admin Menu Editor.

Requirements:

  1. The Admin Menu Editor Pro plugin should be installed and activated in the Version you are working in.
  2. To utilize the Tenant Lifecycle Hooks, we’ll use the Code Snippets plugin. So make sure you install that as well.
  3. In order to license the Admin Menu Editor Pro plugin, you’ll of course need a license for it. Check out Admin Menu Editor Pro to see how to obtain one.

The PHP within a plugin can look different for each tenant, but if you scan the code it might look something like it does with Admin Menu Editor:

				
					add_action('wpcs_tenant_created', 'activate_license_ame_plugin');
function activate_license_ame_plugin()
{
    $licenseManager = $GLOBALS['ameProLicenseManager'];
    $licenseManager->licenseThisSite('YOUR_LICENSE_KEY');
}

Notice how the logic works similarly as for defining a PHP Constant in the wp-config. In both cases, you need to define the ‘NAME’ and the ‘KEY’.

With Admin Menu Editor, ‘NAME’ is ameProLicenseManager with the ‘KEY’ YOUR_LICENSE_KEY something that you have received.

If you’re a PHP develop or generally not afraid / interested in creating your own PHP snippet: try it out for yourself.

Again, our recommendation is to write to Support and ask them how to automate the activation of the license key with PHP. Optionally, send them these recipes so that they can get an idea of what you’re looking for.

Final note:

There are certain plugins that just don’t “play nice”.
Elementor Pro is such an example. The free version of Elementor does not create any problems when you create new tenants. However, activating Elementor Pro requires you to access the WP Admin and click through to the Elementor website. As far as we can tell, Elementor registers the Pro license to your domain.
We’ve not yet received a solution to this from Elementor. We invite you to write to their Support to ask for one. Together we can create enough waves to push it higher on their to-do list 😊

How to install and activate new plugins for existing tenants

As stated above, if you’re interested in learning how to install and activate new plugins for existing tenants, watch this webinar.

We’ll demonstrate how to create a must-use plugin to automatically activate new plugins in existing tenants. Then, we’ll show you how to create your own plugin to activate the license key.

If you find this hard or rather just want to pay someone to set this up for you: get in touch with our support team. We’ll gladly help you out.

Share a PHP snippet, earn a discount

If you happen to create or receive a Lifecycle Hook with a PHP snippet, please share it in our FB community. Your contribution is much appreciated and might earn you a nice discount!

Build a WP-based SaaS today

Start a 30-day free trial – Cancel anytime