Preloading Advanced Custom Fields Pro license key

Most paid plugins require a key to run. Some popular plugins also allow you to preload their key from the wp-config.php file. The following is an example of preloading keys for Akismet, Gravity Forms and WP Smush Pro.

define( 'WPCOM_API_KEY', 'XXXXXXXXXX' );
define( 'GF_LICENSE_KEY', 'XXXXXXXXXX' );
define( 'WPMUDEV_APIKEY', 'XXXXXXXXXX' );

Unfortunately Advanced Custom Fields Pro doesn’t preload keys

Worse yet, ACF Pro seems to drop its key every time you switch URLs. That includes the slightest of changes like switch from http to https. Not to mention staging and development sites. To solve this problem I decided to find my own solution.

Screen Shot 2017-04-21 at 11.30.30 PM.png
ACF stores it’s key serialized along with the current url. Here is what a deactivated key looks like on my development environment after I deactivate it.

Start by adding ACF Pro’s key to wp-config.php

Much of this came from @jacobdubail and adapted for my use case. The ACF key should looks something like this in wp-config.php define( 'ACF_PRO_KEY', 'XXXXXXXXXX' );

Next trigger the preload script when running wp-cli

Now that the license is defined you can preload into the database by running the below code. I decided I wanted to preload the ACF Pro key every time I run a wp search-replace but only if ACF is running and the key was found in wp-config.php. The following is the relevant parts taking from my Anchor Hosting client plugin which is preinstalled on all of my client websites.

function anchor_acf_auto_set_license_keys_trigger() {
    // Running after_invoke:search-replace runs too early for ACF to catch new urls
    // This makes sure the license is updated after the fact
    WP_CLI::runcommand( "eval 'anchor_acf_auto_set_license_keys();'" );
}

// Preload ACF key from wp-config.php into database
function anchor_acf_auto_set_license_keys() {

  // Check if ACF is running and ACF Pro key found in wp-config.php
  if ( defined('ACF_PRO_KEY') && is_plugin_active( 'advanced-custom-fields-pro/acf.php' ) ) {

        // Loads ACF plugin
        include_once( ABSPATH . 'wp-content/plugins/advanced-custom-fields-pro/acf.php');

        // connect
        $post = array(
            'acf_license'   => ACF_PRO_KEY,
            'acf_version'   => acf_get_setting('version'),
            'wp_name'       => get_bloginfo('name'),
            'wp_url'        => home_url(),
            'wp_version'    => get_bloginfo('version'),
            'wp_language'   => get_bloginfo('language'),
            'wp_timezone'   => get_option('timezone_string'),
        );

        // connect
        $response = acf_updates()->request('v2/plugins/activate?p=pro', $post);

        // ensure response is expected JSON array (not string)
        if( is_string($response) ) {
            $response = new WP_Error( 'server_error', esc_html($response) );
        }

        // error
        if( is_wp_error($response) ) {
            WP_CLI::log( $response->get_error_message() );
        }

        // success
        if( $response['status'] == 1 ) {
            acf_pro_update_license( $response['license'] ); // update license
            WP_CLI::log( "ACF Pro: ". wp_strip_all_tags($response['message']) ); // show message
        } else {
            WP_CLI::log( "ACF Pro: ". wp_strip_all_tags($response['message']) ); // show message
        }

  } // ends if defined('ACF_PRO_KEY')

}

// Runs 'Preload ACF key' if WP-CLI runs a search-replace
if ( defined( 'WP_CLI' ) && WP_CLI ) {
    WP_CLI::add_hook( 'after_invoke:search-replace', 'anchor_acf_auto_set_license_keys_trigger' );
}