Support

Account

Home Forums ACF PRO acf-json fields not loading from parent theme

Solved

acf-json fields not loading from parent theme

  • I have created a parent theme and a number of child themes. The child themes are minor style variations of the parent theme, thus all functionality including custom fields definitions resides in the parent theme.

    I am using the acf-json folder to be able to keep the custom fields defined with the theme and not in the database of the various deployments. However when I activate the respective child themes the fields from the acf-json folder in the parent theme are not loaded.

    Maybe this is by design, but to me it would be the expected behavior to not lose the fields defined in the parent theme. It seems that changing line 8 and 9 of core/json.php to use get_template_directory instead of get_stylesheet_directory solves the problem, but I don’t know what the broader consequences of this change might be.

    acf_update_setting('save_json', get_template_directory() . '/acf-json');
    acf_append_setting('load_json', get_template_directory() . '/acf-json');

    Any ideas?

  • I think it would be safer to use the filters for this. In my setup I want to use both the child theme and parent theme acf-json, but only save to the child theme acf-json folder:

    add_filter('acf/settings/save_json', function() {
    	return get_stylesheet_directory() . '/acf-json';
    });
    
    add_filter('acf/settings/load_json', function($paths) {
    	$paths = array(get_template_directory() . '/acf-json');
    
    	if(is_child_theme())
    	{
    		$paths[] = get_stylesheet_directory() . '/acf-json';
    	}
    
    	return $paths;
    });

    Documented here.

    I hope it helps 🙂

  • It doesn’t work for me.
    1. I’ve created acf-json dir in the root and child theme
    2. I’ve applied filters as above
    3. ACF still loads only the fields from the current theme.

  • Thanks @johanhermansson for the snippet, works like a charm.


    @wube
    you’ll need to put this code in the parent themes functions.php and of course make sure you have ACF 5.n pro installed.

    I can confirm that above snippet works!

  • Hello all, just to confirm the above snippet by @johanhermansson worked for me too. Cheers man!

  • Thanks @johanhermansson for the snippet. If somebody needs to allow modify fields from child theme this snippet works for me:

    add_filter('acf/settings/load_json', function($paths) {
        $paths = array(get_template_directory() . '/acf-json');
    
        if(is_child_theme()){
            $paths = array(
                get_stylesheet_directory() . '/acf-json',
                get_template_directory() . '/acf-json'
            );
    
        }
    
        return $paths;
    });
  • I’ve been using this for a while without issue. I sometimes modify a field group imported from the parent within the child theme. It simply saves a new acf-json file into the child theme.
    This used to work fine but I my posts are suddenly only rendering the group as defined in the parent. So I had to flip the load order though I have no idea why.

    
    add_filter('acf/settings/load_json', function($paths) {
      $paths = array();
    
      if(is_child_theme())
      {
        $paths[] = get_stylesheet_directory() . '/acf-json';
      }
      $paths[] = get_template_directory() . '/acf-json';
    
      return $paths;
    });
    
  • @joelstransky Interesting. I don’t remember where I used this in the first place, but I test it when/if I do 😀

  • Has anyone been able to do something similar with the save function?

    I copied @johanhermansson suggestion into the save filter, but am getting this error:
    [18-Mar-2019 22:20:21 UTC] PHP Warning: rtrim() expects parameter 1 to be string, array given in .../wp-includes/formatting.php on line 2660

    add_filter('acf/settings/save_json', 'my_acf_json_save_point');
    function my_acf_json_save_point( $path ) {
    	$paths = array(THEME_DIR.'/config/acf-configs/acf-json');
    
    	if(is_child_theme()) {
    		//If we are in the child theme, check for child theme specific json files
    		$paths[] = CHILD_THEME_DIR.'/config/acf-configs/acf-json';
    	}
    
    	// return
    	return $paths;
    }
  • @okadots The acf/settings/save_json filter needs a single path, where the JSON files should be saved while working in admin.

    But if you want to load JSON files from multiple folders, you should use the acf/settings/load_json and return an array of paths 🙂

    I’m guessing you want something like this:

    add_filter('acf/settings/load_json', 'my_acf_json_load_point');
    function my_acf_json_load_point( $paths = array() ) {
    	$paths = array( THEME_DIR . '/config/acf-configs/acf-json' );
    
    	if ( is_child_theme() ) {
    		$paths[] = CHILD_THEME_DIR . '/config/acf-configs/acf-json';
    	}
    
    	return $paths;
    }

    And save to child theme dir when working in child theme?

    add_filter('acf/settings/save_json', 'my_acf_json_save_point');
    function my_acf_json_save_point( $path = '' ) {
    	$path = THEME_DIR . '/config/acf-configs/acf-json';
    
    	if ( is_child_theme() ) {
    		$path = CHILD_THEME_DIR . '/config/acf-configs/acf-json';
    	}
    
    	return $path;
    }
  • @johanhermansson the load is working perfectly – I used what you had posted earlier. The save function is where it gets tricky.

    What you just posted is working for me for now, thanks for the quick reply.

Viewing 11 posts - 1 through 11 (of 11 total)

The topic ‘acf-json fields not loading from parent theme’ is closed to new replies.