A while back, Carrie Dils asked me in the Genesis Slack (something I, sadly, have used very little) if I knew of a way to integrate the Software Licensing add-on for Easy Digital Downloads with Genesis. In a nutshell, it’s actually quite simple if you’re familiar with the coding concepts involved. In this tutorial, I’m going to explain how to integrate the EDD Software Licensing add-on with the built-in Genesis theme settings so you can easily license your Genesis plugins and child themes.
To start, this post is intended more for developers. I’m going to assume you know how to get the plugin set up and the updater class (and sample code) included in your child theme or plugin. If you need help with these tasks, check out the Software Licensing documentation. I’m also going to assume you’re somewhat familiar with the WordPress Options API.
License Setting in Child Theme or Plugin
The Software Licensing code you’ll need to focus on differs between plugins and themes but, overall, the concept is the same. When integrating Software Licensing in your plugin or theme, the sample code included for both themes and plugins adds a top-level (includes a dashboard menu link) menu page to the WordPress admin for the purpose of having the user save their license key. This added menu page is helpful for getting you started when you need a new menu page but not so much otherwise. When using it with Genesis (and many other plugins/themes, for that matter), we already have an area in the admin that we want to add this feature to so, in our case, this menu page is unnecessary and we don’t want to add it (better for UX). Therefore, you need to strip out the code that adds the menu page.
For plugins, remove the
edd_sample_license_page() functions and their associated add_action()’s in the edd-sample-plugin.php file. These functions add the menu item and the menu page’s content, respectively. The rest of the code in that file remains the same for now.
For themes, you need to remove the
license_page() methods in theme-updater-admin.php. This is doing the same thing as with plugins. Also, don’t forget to remove the associated add_actions()’s in the class
__construct() method, as this code is object-oriented rather than procedural.
Once you’ve removed the code that adds the menu page, check for any errors you may have made and to ensure there is, in fact, no new menu page in the dashboard. Assuming everything is kosher, we can move on to creating our own way of having the user enter their license key. Since we’re using Genesis, we’re going to say we want to have that option right along with the rest of the Genesis theme settings (i.e. Custom Feeds, Default Layout, etc). Bill Erickson did a great walkthrough of creating theme options in Genesis and I’ll be using the same process so you can check out his tutorial to learn more.
The code below adds a new meta box to the Genesis theme settings page. The meta box contains a text input that will accept the user’s license key.
Make sure to modify the prefixes and text to your requirements. Once you have this code inserted, the Genesis theme settings page will look something like this:
Retrieving the License Key Option
Next, we need to account for retrieving the user’s license key. When the user enters his or her license key in the License Key setting field and clicks Save, it is saved to the wp_options table in their installation’s database. Normally, we’d use
get_option() to retrieve a saved option but, because we’re using Genesis’s API to save the value, the license key is stored in the “genesis-settings” option, which is an array of all the individual Genesis settings. This means that get_option() won’t work like normal so we need to modify some additional sample code to properly retrieve the license key. You have two options:
get_option()but use array notation to retrieve the proper array element. In this tutorial, we saved the license key to an option called “prefix_license_key” (technically it was saved as an array element with a key of “prefix_license_key”). To access the value using
get_option(), we’d need to do the following:
$genesis_settings = get_option( 'genesis-settings' ); $license = trim( $genesis_settings['prefix_license_key'] );
genesis_get_option(), which is a wrapper for get_option() that saves you the added step of plucking the element you need out of the genesis-settings option array. Personally, I’d say this is the easier option because all you need to do is change every instance of
get_option( 'prefix_license_key' )to
genesis_get_option( 'prefix_license_key ). For example:
$license = trim( genesis_get_option( 'prefix_license_key' ) );
Either two of these retrieval methods will work but emphasize this step because if done incorrectly, your customers’ license keys will not properly validate with your licensing system (EDD Software Licensing). Also, one important thing to note is how the license key option is retrieved according to its name. By default, the EDD Software Licensing sample code for plugins retrieves the exact option name (string) you pass it. If you tell it to retrieve “prefix_license_key”, it’s going to retrieve “prefix_license_key”. The sample code for themes, however, retrieves the option by concatenating the theme slug (a property defined at the beginning of the class) with “_license_key”. Therefore, when naming your option for a plugin, you can basically use whatever you want. When naming your option for a theme, stick with the “[theme-slug]_license_key” format unless you’re willing to change that in the sample code as well.
As you see, integrating Easy Digital Downloads Software Licensing with Genesis is not incredibly complex. All you’re doing is 1.) modifying the sample code for either your plugin or (child) theme so that it doesn’t add a separate admin page, 2.) adding a custom setting and meta box to the existing Genesis theme settings page to accept the user’s license key, and 3.) altering how the license key option is accessed since it’s stored as an array element in an existing option rather than its own separate option. Once your plugin or theme is properly passing a user’s license key to the rest of the licensing code (i.e. activating, checking and deactivating), you’re in business.
Those are the nuances specific to using EDD Software Licensing with the Genesis Framework. I hope it helps!