Support

Account

Home Forums General Issues Bundling ACF in plugin

Solving

Bundling ACF in plugin

  • I am trying to bundle ACF in a plugin as per documentation but having issues.
    https://www.advancedcustomfields.com/resources/including-acf-in-a-plugin-theme/

    No problems inserting in the theme.
    What changes do I need to make for the plugin load?

    Eg:

    function sh_setup_acf()
    {
    	// Include ACF.
    	// 1. customize ACF path
    	add_filter('acf/settings/path', 'my_acf_settings_path');
    	 
    	function my_acf_settings_path( $path )
    	{
    	 
    	    // update path
    	    $path = plugin_dir_path() . 'myplugin/vendors/advanced-custom-fields-pro/';
    	    
    	    // 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_path() . 'myplugin/vendors/advanced-custom-fields-pro/';
    	    
    	    // return
    	    return $dir;
    	}
    	
    	// 3. Hide ACF field group menu item
    	add_filter('acf/settings/show_admin', '__return_true');
    	
    	// 4. Include ACF
    	include_once( plugin_dir_path() . 'myplugin/vendors/advanced-custom-fields-pro/acf.php' );
    }
    
    add_action( 'plugins_loaded', 'sh_setup_acf');
  • More than likely ACF has called those hooks before ‘plugins_loaded’, but that’s just a guess.

    Try not adding the filters inside of another filter. You can add filters when the file that function is in is loaded.

    Also, functions added inside of function become top level function in ACF, however… A function inside of a filter, well, the filter is called inside a class in WP and if I’m not mistaken, those functions become part of the class. It could be that just moving the functions outside of your action filter will correct the problem.

  • Sorry the code should have been

    function my_acf_settings_path( $path )
    {
        // update path
        $path = plugin_dir_path() . 'myplugin/vendors/advanced-custom-fields-pro/';
    	    
        // return
        return $path;
    }
    
    function my_acf_settings_dir( $dir )
    {	 
        // update path
        $dir = plugin_dir_path() . 'myplugin/vendors/advanced-custom-fields-pro/';
    	    
        // return
        return $dir;
    }
    
    function sh_setup_acf()
    {
    	// Include ACF.
    	// 1. customize ACF path
    	add_filter('acf/settings/path', 'my_acf_settings_path');
    	
    	// 2. customize ACF dir
    	add_filter('acf/settings/dir', 'my_acf_settings_dir');
    	
    	// 3. Hide ACF field group menu item
    	add_filter('acf/settings/show_admin', '__return_true');
    	
    	// 4. Include ACF
    	include_once( plugin_dir_path() . 'myplugin/vendors/advanced-custom-fields-pro/acf.php' );
    }
    
    add_action( 'plugins_loaded', 'sh_setup_acf');
  • Essentially I am just looking for the standard way of adding ACF to a plugin

  • I don’t know that there is a standard way to set up acf in a plugin other than what is posted.

    But I would try adding the filters for the acf functions without them being in another filter first to see if that corrects the problem you’re seeing.

    
    function my_acf_settings_path( $path )
    {
        // update path
        $path = plugin_dir_path() . 'myplugin/vendors/advanced-custom-fields-pro/';
    	    
        // return
        return $path;
    }
    
    function my_acf_settings_dir( $dir )
    {	 
        // update path
        $dir = plugin_dir_path() . 'myplugin/vendors/advanced-custom-fields-pro/';
    	    
        // return
        return $dir;
    }
    	// Include ACF.
    	// 1. customize ACF path
    	add_filter('acf/settings/path', 'my_acf_settings_path');
    	
    	// 2. customize ACF dir
    	add_filter('acf/settings/dir', 'my_acf_settings_dir');
    	
    	// 3. Hide ACF field group menu item
    	add_filter('acf/settings/show_admin', '__return_true');
    	
    	// 4. Include ACF
    	include_once( plugin_dir_path() . 'myplugin/vendors/advanced-custom-fields-pro/acf.php' );
    
Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘Bundling ACF in plugin’ is closed to new replies.