Support

Account

Home Forums ACF PRO Problem including ACF Pro in custom plugin

Solving

Problem including ACF Pro in custom plugin

  • Following this giude: http://www.advancedcustomfields.com/resources/including-acf-in-a-plugin-theme/

    I’ve got a plugin that is calling a script. It checks if the class “acf” exists. If it does not exist, it includes ACF Pro from the plugin files. This should let you use ACF Pro as a plugin if you want to keep it updated.

    So I’ve got it figured out, with the help of a couple global vars to transmit the paths:

    function limelight_include_acf_files( $acf_path, $acf_url, $hide_menu = false ) {
    	global $ld_acf_paths;
    
    	$ld_acf_paths = array(
    		'path' => $acf_path,
    		'url' => $acf_url,
    	);
    
    	// 1. customize ACF path
    	add_filter('acf/settings/path', 'limelight_acf_settings_path');
    
    	// 2. customize ACF dir
    	add_filter('acf/settings/dir', 'limelight_acf_settings_dir');
    
    	// 3. Hide ACF field group menu item
    	if ( $hide_menu ) add_filter('acf/settings/show_admin', '__return_false');
    
    	// 4. Include ACF
    	include_once( $acf_path . 'acf.php' );
    
    	var_dump($ld_acf_paths);
    	var_dump($acf_path . 'acf.php');
    	var_dump(acf()->settings);
    }
    
    function limelight_acf_settings_path( $path ) {
    	global $ld_acf_paths;
    	return $ld_acf_paths['path'];
    }
    
    function limelight_acf_settings_dir( $dir ) {
    	global $ld_acf_paths;
    	return $ld_acf_paths['url'];
    }

    See the var_dumps at the end of “limelight_include_acf_files”? They report the following:

      'path' => string 'G:\Web Development\Wamp Server\www\sports\wp-content\plugins\limelight-ezad-replacement\acf/advanced-custom-fields-pro/' (length=119)
      'url' => string 'http://localhost/sports/wp-content/plugins/limelight-ezad-replacement/acf/advanced-custom-fields-pro/' (length=101)

    string 'G:\Web Development\Wamp Server\www\sports\wp-content\plugins\limelight-ezad-replacement\acf/advanced-custom-fields-pro/acf.php' (length=126)

    array (size=21)
      'name' => string 'Advanced Custom Fields PRO' (length=26)
      'version' => string '5.3.2.2' (length=7)
      'basename' => string 'limelight-ezad-replacement/acf/advanced-custom-fields-pro/acf.php' (length=65)
      'path' => string 'G:\Web Development\Wamp Server\www\sports\wp-content\plugins\limelight-ezad-replacement\acf\advanced-custom-fields-pro/' (length=119)
      'dir' => string 'http://localhost/sports/wp-content/plugins/limelight-ezad-replacement/acf/advanced-custom-fields-pro/' (length=101)
      'show_admin' => boolean true
    ......
      'pro' => boolean true
    }

    This all looks good. The directory and URLs are correct (barring the backslash/forward slash mixup, but PHP doesn’t mind that). The ACF function is pulling in the settings properly, as we can see at the end.

    All is well so far, but I get a ton of errors on the admin.

    ( ! ) Notice: Trying to get property of non-object in G:\Web Development\Wamp Server\www\sports\wp-content\plugins\limelight-ezad-replacement\acf\advanced-custom-fields-pro\api\api-helpers.php on line 19

    Line 19: $settings = acf()->settings;

    ( ! ) Warning: Attempt to modify property of non-object in G:\Web Development\Wamp Server\www\sports\wp-content\plugins\limelight-ezad-replacement\acf\advanced-custom-fields-pro\api\api-helpers.php on line 72

    Line 72: acf()->settings[ $name ] = $value;

    It’s as if acf() isn’t available anymore.

    Any ideas?

    Here is a copy of the plugin so far. Which is almost nothing. I removed ACF Pro itself from the “acf” folder so you’ll need to put that in t here yourself.

    https://drive.google.com/file/d/0B91C2X4n3qgRaW5aN0lETnY3Vm8/view?usp=sharing

  • Hi

    I have the same issue here

    Did you success to solve it?

    Michael

  • No, unfortunately. I’ve tried a few things to get it to work but in the end I had given up.

  • Hi

    Ive succeeded to solve this issue at least in my end

    Heres the code:

    
    // 1. customize ACF path
    add_filter('acf/settings/path', 'my_acf_settings_path');
    
    function my_acf_settings_path( $path ) {
    
    // update path
    $path = dirname( __FILE__ ) .  '/libs/acf/';
    
    // return
    return $path;
    
    }
    
    // 2. customize ACF dir
    add_filter('acf/settings/dir', 'my_acf_settings_dir');
    
    function my_acf_settings_dir( $dir ) {
    
    // update path
    $dir = plugin_dir_url( __FILE__ ) . 'libs/acf/';
    
    // return
    return $dir;
    
    }
    
    // 3. Hide ACF field group menu item
    //add_filter('acf/settings/show_admin', '__return_false');
    
    // 4. Include ACF
    include_once( 'libs/acf/acf.php' );
    
    
Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Problem including ACF Pro in custom plugin’ is closed to new replies.