Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Define the Advanced Custom Fields Pro license key with a constant
#!/bin/bash
# Setup ACF Pro to use a license key in wp-config
#
# 1. Place your license key in a file called .acf-license-key somewhere your ssh user has access to it.
# Default is in your home directory: ~/.acf-license-key
# Change the setting below if you need to put it somewhere else
# 2. install this file in /usr/local/bin or someplace in your PATH
# 3. make sure you have WP CLI installed
# 4. run acf-install-license from the root of your wordpress install
# set the license key in wp-config
ACF_LICENSE_KEY=`cat ~/.acf-license-key`
wp config set ACF_PRO_LICENSE $ACF_LICENSE_KEY
# install the mu-plugin
mkdir -p wp-content/mu-plugins
cd wp-content/mu-plugins
wget -O acf_pro_license_constant.php https://gist.githubusercontent.com/petertwise/eeb7dea2531ad204a346e42faba0179a/raw/acf_pro_license_constant.php
cd ../..
wait
echo "✅ License and Plugin Installed
"
<?php
/*
Plugin Name: ACF Pro License Constant
Plugin URI: https://gist.github.com/petertwise/eeb7dea2531ad204a346e42faba0179a
Description: Enables entering ACF Pro plugin activation codes via wp-config.php
Version: 1.1.2
Allows storing license key info for Advanced Custom Fields Pro in wp-config.php
Especially handy if you're constantly pulling database copies between production/staging/local
and invalidating the license in the process
Usage: Place this in wp-config.php
define('ACF_PRO_LICENSE', 'yourkeyhere' );
*/
/*
ACF Pro License Constant is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
any later version.
ACF Pro License Constant is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with ACF Pro License Constant. If not, see https://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html.
*/
function squarecandy_acf_pro_license_autosave() {
/*
bail out if:
- setting in wp-config doesn't exist
- setting in wp-config is empty
- ACF Pro is not installed and activated
- the existing key matches the setting
*/
if ( ! defined( 'ACF_PRO_LICENSE' ) || empty( ACF_PRO_LICENSE ) || ! function_exists( 'acf_pro_get_license_key' ) || acf_pro_get_license_key() === ACF_PRO_LICENSE ) {
return;
}
// everything below is modified from advanced-custom-fields-pro/pro/admin/admin-updates.php in the activate_pro_licence function
// Connect to API.
$post = array(
'acf_license' => ACF_PRO_LICENSE,
'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'),
);
$response = acf_updates()->request('v2/plugins/activate?p=pro', $post);
// Check response is expected JSON array (not string).
if( is_string($response) ) {
$response = new WP_Error( 'server_error', esc_html($response) );
}
// Display error.
if( is_wp_error($response) ) {
wp_die( $response->get_error_message() );
}
// On success.
if( $response['status'] == 1 ) {
// Update license.
acf_pro_update_license( $response['license'] );
// Refresh plugins transient to fetch new update data.
acf_updates()->refresh_plugins_transient();
// Show notice.
acf_add_admin_notice( $response['message'], 'success' );
// On failure.
} else {
// Show notice.
acf_add_admin_notice( $response['message'], 'warning' );
}
}
add_filter( 'admin_init', 'squarecandy_acf_pro_license_autosave' );
@dotZak
Copy link

dotZak commented Aug 4, 2021

What is $this on line 52?

@petertwise
Copy link
Author

petertwise commented Aug 4, 2021

Good catch @dotZak - copypasta from something class-based, I'm sure. See revision.

@joshmoto
Copy link

joshmoto commented Oct 7, 2021

This is tremendous! Thank you so much for pursuing this problem. Finally I can use a ACF licence constant in my environments. Great job!

I have utilised your acf_pro_license_constant.php plugin file by including it my mu-plugins folder.

And simply defining ACF_PRO_LICENSE constant my wp-config.php files and my local docker-compose.yml file.

@subfighter3
Copy link

subfighter3 commented Oct 19, 2021

Hi there, thanks for the very useful plugin!

Unfortunately, after the last update, I get the following message as soon as I log in in the wp admin panel: cURL error 60: SSL certificate problem: certificate has expired

The certificate has been updated and I don't understand how to use the plugin in my local environment anymore!

Anyone experiencing the same issue?

here the Call Stack:

    wp_die()
    wp-content/mu-plugins/acf_pro_license_constant.php:68

    squarecandy_acf_pro_license_autosave()
    wp-includes/class-wp-hook.php:303

    do_action('admin_init')
    wp-admin/admin.php:175

@subfighter3
Copy link

subfighter3 commented Oct 19, 2021

I found this solution which fix the problem for me: https://wp-kama.com/note/error-making-request-wordpress

Hi there, thanks for the very useful plugin!

Unfortunately, after the last update, I get the following message as soon as I log in in the wp admin panel: cURL error 60: SSL certificate problem: certificate has expired

The certificate has been updated and I don't understand how to use the plugin in my local environment anymore!

Anyone experiencing the same issue?

here the Call Stack:

    wp_die()
    wp-content/mu-plugins/acf_pro_license_constant.php:68

    squarecandy_acf_pro_license_autosave()
    wp-includes/class-wp-hook.php:303

    do_action('admin_init')
    wp-admin/admin.php:175

@petertwise
Copy link
Author

petertwise commented Nov 11, 2021

Very pleased that this will now be part of ACF PRO as of 5.11:
https://support.advancedcustomfields.com/forums/topic/pro-license-key-in-config/#post-150028

Because they have chosen the exact same constant, you can just remove this drop in plugin and everything should continue to work.

@joshmoto
Copy link

joshmoto commented Nov 11, 2021

@petertwise this is great news, thanks for the head up 👍🏻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment