Support

Account

Home Forums ACF PRO Local JSON in plugin not load

Solved

Local JSON in plugin not load

  • I’ve a problem with local json and i don’t know, it is a bug.

    I did everything that is on this page:
    https://www.advancedcustomfields.com/resources/local-json/
    But the own folder was used only for saving and not for loading.
    It works only in the master theme – not in my child-themes.

    So I put it in a plugin. Here is my code:

    But it is the same. ACF saves the JSON, but an other installation don’t read this file.

    Where is my mistake?

    
      // save jsonwith fields
      function bks_acf_json_save_point( $path ) {
        // update path
        $path = plugin_dir_path( __DIR__ ) . '/acf-json';
        // return
        return $path;
      }
      
      
      // load json with fields
      function bks_acf_json_load_point( $paths ) {
        // append path
        $paths[] = plugin_dir_path( __DIR__ ) . '/acf-json';
        // return
        return $paths;
      }
      
      add_filter('acf/settings/save_json', 'bks_acf_json_save_point');
      add_filter('acf/settings/load_json', 'bks_acf_json_load_point');
    

    I also tried the PHP variant. Everything is displayed correctly, but the repeat fields are not saved.

  • You need to add the following line to the load function because only 1 path may exists per load/save.

    unset($paths[0]);

    That being said, I think you should look into the issue why it’s not loading from the other site, because this is the default behaviour and should work, so something is blocking it.

  • Hello @beee,

    this didn’t solve the problem.

    I have removed this line, because the json from the template is also not loaded any more.

    Thanks for the try.

  • Well, according to the local json info page, it needs to be unset, so that was step 1.

    What if you dump $paths in the save function ? What does it output ?

  • I do not think so. The info page says that the folder “acf-json” is one of the load points. If you look in the code of ACF, then you see that there are many folders possible. With unset I remove one of these load points. But that is not, what i want. I want many load points.

    In the plugin i want to load the fields for all my sites. In the theme i want to load the fields for a special page.

    Please have also a look at the comment over the line with unset
    // remove original path (optional)
    I don’t want to remove this path. I will use it, too.

    And how i said: It didn’t solve the problem.

    If i use var_dump in acf_json_load_point nothing would be displayed.

  • You are right, I stand corrected.

    Have you tested with a higher or lower priority ?

    Higher
    add_filter('acf/settings/load_json', 'bks_acf_json_load_point', 1);

    Lower
    add_filter('acf/settings/load_json', 'bks_acf_json_load_point', 20);

  • Actually var_dump did a job for me within my function, and in my case revealed that plugin_dir_url( __FILE__ ) was providing a url ending with / so my constructed path of:

    plugin_dir_url( __FILE__ ) . ‘/acf-json’

    (which similar to yours above skrabbel features a / in the final part) was producing an erroneous // in the URL.

    ….frustratingly though this hasn’t resolved my issue (which has similar symptoms to the above – filtering load_json within a plugin failing to then implement the field (group) ), and the above prioritizing doesn’t either.

  • I’m having issues with the synchronization feature as well. The save point works fine. The files get stored in the right place and update each time I save a group. The sync available option, however, does not appear as predicted after I pull relevant changes from my repo onto my other machine (which, in fact, has no field groups in its DB). Neither does the option appear on the original machine after I edit the files directly. I even incremented the modified property.

    These are my save and load points:

    add_filter('acf/settings/save_json', 'p2c_acf_json_save_point');
    function p2c_acf_json_save_point( $path ) {
        $path = plugin_dir_path(__FILE__) . 'fields/acf-json';
        return $path;
    }
    
    add_filter('acf/settings/load_json', 'p2c_acf_json_load_point');
    function p2c_acf_json_load_point( $paths ) {
        unset($paths[0]);
        $paths[] = plugin_dir_path(__FILE__) . 'fields/acf-json';
        return $paths;
    }

    Permissions on the directory are writeable. I use username:www-data as owner:group in my local environment with 775 permissions.

    I also tried adjusting the priority of the filters to no effect. plugin_dir_path() already provides the trailing slash and I verified that the correct directory is pushed to $paths (and the default removed).

    Any ideas?

  • We are having similar issues, but from within a theme. It is like the filter acf/settings/load_json never fires from within functions.php. Even if we put a PHP exit call in the function nothing happens.

  • If your filter is not running then the likely causes are:

    1) Your add_filter('acf/settings/load_json' ..... line of code is never running

    2) Your add_filter('acf/settings/load_json', ..... line of code is run after the hook has fired.

    So the question is “When” is the filter being added?

  • I just found the solution to the problem. My hooks were registered in the functions.php but I did it after having called get_field() a little bit earlier. Calling get_field() initialises ACF and thus the hooks were useless afterwards.

    Seems so simple now, but was quite hard to grasp…

  • Hello all. I understand this topic seems closed, but I’m hoping you folks can help me resolve this. I want to load my field group json file which lives in a folder within my plugin. Let me explain my setup.

    There are only two active plugins, ACF and my very simple custom plugin (which only attempts to load a json field group).

    Here’s a look at the .json file. Super simple for testing purposes. And this works if I build it out in the ACF interface. It displays on every page. I exported this group’s json and removed it from the ACF interface. So at this point you don’t see this group in ACF.

    [
        {
            "key": "group_63bc2ff8023f5",
            "title": "Test Field Group",
            "fields": [
                {
                    "key": "field_63bc2ff682571",
                    "label": "Test Message",
                    "name": "",
                    "aria-label": "",
                    "type": "message",
                    "instructions": "",
                    "required": 0,
                    "conditional_logic": 0,
                    "wrapper": {
                        "width": "",
                        "class": "",
                        "id": ""
                    },
                    "message": "If you're on a page you should see me. Do you?",
                    "new_lines": "wpautop",
                    "esc_html": 0
                }
            ],
            "location": [
                [
                    {
                        "param": "post_type",
                        "operator": "==",
                        "value": "page"
                    }
                ]
            ],
            "menu_order": 0,
            "position": "normal",
            "style": "default",
            "label_placement": "top",
            "instruction_placement": "label",
            "hide_on_screen": "",
            "active": true,
            "description": "",
            "show_in_rest": 0
        }
    ]

    I placed the following code in my main plugin file.

    
    add_filter('acf/settings/load_json', function($paths){
      $paths[] = WP_PLUGIN_DIR .'/gmr-ssp/acf-json';
      return $paths;
    });

    Uploaded everything. Refresh. Nothing. It does not load the field group on the page. The following DOES run and prints the correct paths to the folder where my field group json file resides.

    add_filter('acf/settings/load_json', function($paths){
      $paths[] = WP_PLUGIN_DIR .'/gmr-ssp/acf-json';
      print_r($paths);
      die();
      return $paths;
    });

    I just don’t see what I’m missing here.

    Any insight would be greatly appreciated.

    Thanks!

  • I wouldn’t recommend using WP_PLUGIN_DIR because if you use a custom setup with a different ABSPATH (like roots/bedrock), then it will fail already.

    I would use something like plugin_basename( __FILE__ ).

    Don’t think this will fix your issue (depending on your setup), but it’s good practice to not use WP_PLUGIN_DIR imo.

  • Thanks for the reply @beee. I confirmed the path to the directory is correct. Hmm. I’ve just hit a wall. How can I confirm the add_filter is firing when it should? What else could the issue be?

  • Try setting the priority of your filter to > 10

    
    add_filter('acf/settings/load_json', function($paths){
      $paths[] = WP_PLUGIN_DIR .'/gmr-ssp/acf-json';
      return $paths;
    }, 20);
    
  • Hi @john-homenuke. Thanks for the reply.

    I set the priority to 20. That did not do the trick, unfortunately.

    I reconfirmed the path to the json directory is correct. I confirmed that chmod is 755 for the json directory. There is no plugin interference (no other plugins except ACF PRO and my little plugin here are active). The json file does work when imported using the ACF interface. For the heck of it I tried the acf/settings/save_json feature which allows you to save to a set directory. That works fine. There’s nothing unique about my case. Hmm. I’m stumped.

    It does make me wanna confirm this hook is firing at the correct time. How can I ensure it’s not firing after ACF has been initiated? Right? That would be an issue to resolve although I suspect it’s not the case here being that this is the only thing loading on my plugin and there are no other plugins active. Hmm.

  • This particular hook can only fire after ACF has initialized, so that should not be an issue.

    If you cannot import the JSON file to the site using the ACF interface then that would indicate there’s something wrong with your JSON file. Did you create this file yourself?

    ACF does not have a save location for field groups, are you using another plugin to achieve this?

  • Hey @john-homenuke. I did not create the json myself. I can import the json using the ACF interface. And after importing the json file I do see the field group at the correct location (any page post type). So that json file works.

    Besides ACF Pro and my plugin there are no other active plugins.

    ACF doesn’t have a save location for field groups? Hmm. Well I instructed ACF to save the json file for field groups to a folder within my plugin using add_filter('acf/settings/save_json' and that worked. Despite this feature not being directly related to the loading of json I wanted to test it to just ensure at least it worked.

    I understand it wouldn’t save any loaded groups because you can’t see the loaded json/field groups in the interface. I had other field groups available in the interface. When they were saved ACF saved them to the folder I set.

    It’s an odd thing. This is a pretty simple implementation, very little code, in a very simple setup, nothing unique, etc. It has to be something absurdly obvious or some tiny detail that’s alluding me, but I can’t imagine what. I’m out of ideas. I even tried this same setup on a different website. Nothing. It will not load the json from the folder I set.

  • okay, I thought you saw some kind of setting other than using the acf/settings/save_json hook. Be aware that this will save all field groups to that location.

    Anyway, I don’t see any reason why it is not working if the path is correct and the filter is running.

  • I discovered the issue…

    I didn’t manually create the field group I wanted to load via my plugin. I created it using ACF then exported it. When you export json from the ACF interface it includes a opening bracket at the top and a closing bracket at the bottom. See my exported json code above. These brackets need to be removed if you intend to use the json file for loading. I removed those brackets and it works.

    Wow. I suggest that ACF does not include those brackets on export.

    I hope others can benefit from my turmoil.

    Thanks to @beee and @john-homenuke for your input.

  • The export is meant to be imported, the export file can have multiple field groups, that is what the bracket is for, to denote an array in the JSON.

    When doing this I generally create an acf-json folder in the theme and then when I’m done I move that file to my plugin and then delete the field group form the site and then load that field group from my plugin.

  • @john-homenuke, Got it. Your method sounds like a solid approach which is the same approach I took. I just got tripped up on those brackets. I take it lots of people use that approach. I’m surprised I’m the only one (I could find) that experienced the issue with the brackets.

    So instead of suggesting that ACF remove the brackets from export I suggest that ACF understand the brackets when loading. Apparently ACF understands them on import.

    Thanks!

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

The topic ‘Local JSON in plugin not load’ is closed to new replies.