Home Forums ACF PRO Conditionally Load .json files


Conditionally Load .json files

  • I’m building a theme that is conditionally loading post types via an options panel that I built with ACF. I’m trying to figure out a way to conditionally load certain .json files using the same options panel.

    I want to have a post type with two different data models.

    Post type: Inventory
    Data type: Car or Truck

    I want to give the admin a way to choose whether to load the car model or truck model, but do it from the options panel for a theme wide option and not have to choose the option every time they create a new post. The company will either be selling passenger cars/trucks or commercial trucks, not both. Hence the reason for locking it down like that.

    The conditional registering of the post types was pretty simple, but I’m struggling to get the json files to conditionally load, since they all load at initialization.

    Any ideas how to go about doing this?

  • add_filter('acf/settings/load_json', 'my_acf_json_load_point');
    function my_acf_json_load_point( $paths ) {
        $custom-json-folder = '';//PUT HERE YOUR PATH LOADER BASED ON OPTIONS
        $paths[] = get_stylesheet_directory() . '/my-custom-folder/'.$custom-json-folder;
        // return
        return $paths;
  • or the second way
    add .htaccess to your local json folder with AddHandler php-script .json
    then create your local.json with php code that dynamicly generate it’s contents based on your options

  • I should have clarified… I had already tried loading via the filter.

    here’s what I had that I couldn’t get to work.

    // Load Data Model type for inventory
    add_filter('acf/settings/load_json', 'my_acf_json_load_point');
    function my_acf_json_load_point( $paths ) {
        $datamodel = get_field('inventory_data_model', 'options');
        // append path
        if($datamodel == 'Car') {
          $paths[] = get_stylesheet_directory() . '/acf-json/car';
        } elseif($datamodel == 'Truck') {
          $paths[] = get_stylesheet_directory() . '/acf-json/truck';
        // return
        return $paths;

    Does the path have to be outside of the acf-json folder? I didn’t try that.

  • you have to set path[0] i guess
    your $paths[] = get_stylesheet_directory() . '/acf-json/truck';;
    just puts another array array element to $paths but ACF as i know uses just $paths[0]

    add unset($paths[0]) at first line of your function

  • Well I don’t want to overwrite the entire array, I’m still using the base acf-json path. I just want to conditionally add the new ones, that’s why omitted the unset.

  • During ACF’s initialization procedure, all .json files within the acf-json folder will be loaded. By default ACF looks for a folder within your theme called acf-json, however, this is only 1 of the load points which can be added.

  • so if it’s not working you may try the second way with json handling as php scripts in acf-json folder

  • Right… it’s says “this is only 1” not “this is THE only 1”

    In the example that is written it even states that the unset is optional because it removes the original pathing… which would be to /acf-json.

    I’m not really into using addhandler and inline conditions because I want to keep the save functionality from my local version for when I push updates/adjustments to the field set. I’d wind up having to keep two versions of the file. That’s messy. If it’s the only option though, I may have to go there.

  • Hi @nickhempsey

    Your code looks good. Have you debugged the $paths array to test it contains the correct data?

  • Hey there @Elliot

    I tried to debug it but I couldn’t get it to return the value to me. I tried adding a print_r($paths) in the load point function, but I’m sure that’s wrong.

    What is the best way to go about doing it?


  • Ok, so I started digging into core/json.php to figure out how to print out the json path array and I found acf_get_setting('load_json'); which I figured is where the paths are stored. However, when I spit out that array I only get 1 array item and it points to the theme acf-json folder, my custom load point is not there.

    I used Debug Bar to verify that the filter was firing and it is indeed (see attached).

    Just to make sure I didn’t mess something up in my code, I used the base filter code that is provided in the documentation and the array still remains intact… just trying to unset the default doesn’t work either. I’m stuck.

  • Ok so I’m making some drastic changes…

    I managed to get the conditions to work fine after I ported these post types to a plugin rather than baking them into the theme. I think in the long run it’ll be easier for myself and the client to manage as plugins anyways.

    Not sure why the filter is not working for the theme, but it works as a plugin… odd.

  • Hi @nickhempsey

    Are you including ACF PRO within your theme? Or is it being used as a plugin?

  • I am running it as a plugin.

  • Hi @nickhempsey

    Thanks mate. Is it possible that the filter code you are using is within another action? Are you adding the filter code to the root of your functions.php file?

  • I too was unable to get this filter working within in my child-theme’s functions.php, but putting it in a plugin worked as well for me. Any ideas why this might be the case?

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

The topic ‘Conditionally Load .json files’ is closed to new replies.