Skip to content

Instantly share code, notes, and snippets.

@petertwise
Forked from polevaultweb/acf_pro_license_constant.php
Last active January 19, 2024 14:48
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save petertwise/eeb7dea2531ad204a346e42faba0179a to your computer and use it in GitHub Desktop.
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' );
@joshmoto
Copy link

@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