Support

Account

Home Forums General Issues Include ACF Pro in premium theme Reply To: Include ACF Pro in premium theme

  • Ha… well I came up with a solution.

    Let’s see… I kind of have to piece it back together. I got it working nearly a year ago and haven’t given it much thought since then.

    First, I’m using register_activation_hook() to check if ACF (free) is active. If it is, I’m copying a file into mu-plugins that contains the following, to deactivate ACF (free):

    
    add_filter('option_active_plugins', function($plugins) {
    	$key = array_search('advanced-custom-fields/acf.php',$plugins);
    	if ($key !== false) {
    		unset($plugins[$key]);
    	}
    	return $plugins;
    });
    

    That register_activation_hook() also generates an admin notice explaining that ACF (free) has been deactivated because my plugin includes ACF PRO. I also have a “Bypass ACF Check” option I created in my plugin’s settings, that will prevent this from running, so they can keep ACF (free) active if they really want to — but explaining this means they lose the ACF PRO features my plugin depends on.

    There’s a corresponding register_deactivation_hook() to delete my file from mu-plugins and re-activate ACF (free), although honestly I don’t think it’s working properly. (I haven’t prioritized fixing it because none of my users has yet complained about it!)

    So, that’s the first step, getting ACF (free) out of the way. Next we have to actually load the PRO version. I’m doing that with the plugins_loaded action, calling this function. (Actually it’s a method in my main class.)

    Note right away here that I’m checking to make sure the site doesn’t already have ACF PRO installed, because if it does I just use that and don’t load my own copy.

    
    		public function acf_init() {
    			// Only load our version of ACF PRO if the full version is not already installed on the site
    			if (!is_plugin_active('advanced-custom-fields-pro/acf.php')) {
    				include_once(plugin_dir_path(__FILE__) . 'vendors/acf/acf.php');
    				add_filter('acf/settings/url', function($url) {
    					return plugin_dir_url(__FILE__) . '/vendors/acf/';
    				});
    				// Hide ACF editing screens (EXCEPT if the site also has ACF free installed)
    				if (!file_exists(WPMU_PLUGIN_DIR . '/r34icspro-no-acf.php')) {
    					add_filter('acf/settings/show_admin', function($show_admin) {
    						return false;
    					});
    				}
    			}
    			// Add our ACF JSON loading path
    			add_filter('acf/settings/load_json', function($paths) {
    				$paths[] = plugin_dir_path(__FILE__) . 'assets/acf-json';
    				return $paths;
    			});
    		}
    

    Some of this may require some modifications (e.g. you may not have the assets/acf-json folder in your plugin, or you may have acf-json somewhere else, and your file in mu-plugins definitely won’t be called r34icspro-no-acf.php); I also stripped out some code that exists for a “dev mode” I have in my plugin.

    I think that’s the heart of it. Then the other big thing I would just recommend is that you should definitely stay on top of keeping your embedded ACF PRO version up-to-date! It isn’t critical for users who have their own version of ACF PRO, but if you’re knocking out their copy of ACF (free), then it’s on you to make sure they’re getting ACF updates. (Of course, some sites might be deliberately holding their versions back, and that’s not something I’ve accounted for here.)