3466560105_0b6f694d4a

Automatically Load WordPress License Keys with wp-config.php

If you develop WordPress websites as frequently as I do, you probably find yourself using a core set of go-to plugins for almost every project you take on.  And If any of these plugins are of “premium” or “commerical” variety, you’ll usually be faced with the repetitive task of entering in their license keys each and every time you want to set up a new website.

For a while I would circumvent this task by cloning a pre-existing database in order to carry over all settings (including my license keys), but I ultimately found that it required more effort than it’s worth.  What I really wanted was a way to register all of my premium plugins and extensions automatically whenever I set up a new WordPress installation, without needing to use a pre-existing database.

I was pleased to find that this is in fact really easy to accomplish, simply by hard-coding license keys into the wp-config file of the WordPress installation.   In this post I will outline exactly how you can take advantage of this technique on your own websites.

Determining Plugin Support

There are some plugins that support hard-coding of license keys by default.  Two of such plugins that I use personally are Akismet and Gravity Forms.  These plugins are pre-configured to look for certain constants that store the license key, and as a result, if that constant is defined in WordPress the plugin will automatically handle registering the license for you upon activation.

The easiest way to determine if a plugin supports hard-coded license keys is to simply check the source code for mentions of “license” or “key”, and identify the appropriate constant that the plugin is looking for.  For example, in the Akismet plugin the constant name is WPCOM_API_KEY, and for Gravity Forms the constant name is GF_LICENSE_KEY.  You can also look in the plugin’s documentation to find such values.

Defining License Keys in wp-config

The wp-config.php file that’s included in every WordPress installation is responsible for defining values that help WordPress run.  By default these constants include database login credentials, debug settings, and authentication salts.

But we can also add our own constants to wp-config, and that’s exactly what we’ll want to do in order to hard-code our various license keys.  In wp_config.php, I’m going locate the comment line that says “That’s all, stop editing! Happy blogging.”, and  insert the following block of code just ABOVE that line:

/**
* License Keys
*
* Set license keys for various plugins & extensions.
*
*/
define('GF_LICENSE_KEY', 'your-key-here');
define('WPCOM_API_KEY', 'your-key-here');

What I’ve inserted here are 2 new constants that correspond to the Gravity Forms and Akismet plugins, respectively.  Note that I’ve made sure to use the EXACT constant names specified by the plugins.  The spaces where you see ‘your-key-here’ should be replaced with the specific license key for each plugin.

Now, when either of these plugins is activated on my website, they will recognize that their constant has been defined and automatically use the hard-coded license key to register the plugin.  No more manually inserting license keys!  :)

What about plugins that don’t support hard-coding?

There will always be some plugins that do not have built-in support for hard-coded license keys.  In these instances, it’s still possible to take advantage of hard-coding by writing a custom function to handle the loading of the license key.

One plugin I use frequently that falls into this category is the Options Panel extension for Advanced Custom Fields.  In order to have the feature activated by default in all of my new WordPress installations, I’ve included the following function in my theme’s functions.php file:

Here’s what the function is doing:

  1. Check if the option “acf_options_key” exists in the database.  This is the option which stores the license key for this plugin.  To implement this function for other plugins, you’ll need to check the source code to determine what option name that the plugin uses for storing its license key.
  2. Check if the constant “ACF_OPTIONS_KEY” has been defined somewhere.  This is a constant name I made up for my own purposes, which I’ll use later.  When using this method for other plugins as well you can choose your own constant names.
  3. If the option “acf_options_key” does NOT yet exist in the database, AND the constant ACF_OPTIONS_KEY has been defined, the function will then create the option using the key stored by the constant.
  4. By hooking the function into after_switch_theme, we’ll ensure the function only runs on theme activation, so that we’re not repeatedly trying to make changes to the database.

Now that I’ve got my function set up, I’m going to return to the wp-config.php file and add my new constant:

/**
* License Keys
*
* Set license keys for various plugins & extensions.
*
*/
define('GF_LICENSE_KEY', 'your-key-here');
define('WPCOM_API_KEY', 'your-key-here');
define('ACF_OPTIONS_KEY', 'your-key-here');

Now when my theme is activated or re-activated, my function will find that the ACF_OPTIONS_KEY is defined and use the value to automatically register my Options Panel extension.

That’s all!

Hopefully this guide has provided you with enough information to start taking advantage of hard-coded license keys, a technique which will save you even more time while developing new WordPress websites.  If you have any questions or suggestions please feel free to comment!

Photo by Brenda Starr 

  • http://www.WantedPeterDarraghQuinn.com dan

    Hi Sean,
    I’ve tried the above for the akismet plugin. I put the following at the end of my wp_config.php file where xxxxxx is my key:
    define(‘WPCOM_API_KEY’, ‘xxxxxxxxxxx’);

    I then activated the akismet plugin however I still get the “Akismet is almost ready. You must enter your Akismet API key for it to work” prompt.

    Looking at the akismet plugin source code I see the following suggesting I should hard code it here instead:
    /** If you hardcode a WP.com API key here, all key config screens will be hidden */
    if ( defined(‘WPCOM_API_KEY’) )
    $wpcom_api_key = constant(‘WPCOM_API_KEY’);
    else
    $wpcom_api_key = ”;

    I can hard code it here but I’d like one central area for my keys.I’m using Akismet Version 2.5.6 , wordpress Version 3.4.1, it looks like the wpcom_api_key specified in the wp_config.php isn’t being read? Any ideas? Thanks.

  • Sean Butze

    Hi Dan,

    I’m not sure why it’s not working for you. I have mine placed directly above the line that reads “That’s all, stop editing!” and I’ve tested it multiple times with new installs of 3.4.1.

    PHP constants are typically global so it shouldn’t matter where you define them in the code, but wp-config.php is a nice central location. You could try hard-coding it within the Akismet plugin to see if it works there.

  • http://mojtabamoosavi.com Mojtaba

    hi , i’m in iran
    i want develop a wp plugin and set license key
    help me !
    thanks

  • http://websiteurl deepak kumar

    hello i want wp-plugin with license key option for one domain..

  • http://websiteurl deepak kumar

    please help me guys…………and send me code
    deepak.kumar5357@gmail.com