Support

Account

Home Forums Backend Issues (wp-admin) When is acf/settings/load_json supposed to run?

Solving

When is acf/settings/load_json supposed to run?

  • Hi,
    I’m trying to set up local json sync on a multisite installation. It worked with the default acf-json folder in the child theme, but I’d like to save and load everything from the parent theme. I managed to make it work when it comes to saving (custom fields will get saved to the parent theme folder from all subsites). However, the filter for loading (acf/settings/load_json) doesn’t seem to trigger. Even when I just try to output some strings in the error log.

    I’m not sure what I’m doing wrong since I basically just copy pasted the documentation’s code and only changed the path, but the fields won’t sync.

    Any help would be appreciated! Thank you 🙂

    Here’s what I put in functions.php:

    // changes saving location for custom fields
    add_filter('acf/settings/save_json', 'my_acf_json_save_point');
    function my_acf_json_save_point($path) {
        // update path
        $path = get_template_directory() . '/custom-fields';
        
        // return
        return $path;
    }
    
    // changes loading location for custom fields
    add_filter('acf/settings/load_json', 'my_acf_json_load_point');
    function my_acf_json_load_point( $paths ) {
        error_log('hey!');
        
        // remove original path (optional)
        unset($paths[0]);
        
        // append path
        $paths[] = get_template_directory() . '/custom-fields';
        
        // return
        return $paths;
    }
  • Is it not running at all or is it not loading the field groups correctly?

  • Thank you for your response! It’s not running at all. The text of the second function I try to output in the log doesn’t even show.

  • Do you have WP_DEBUG and WP_DEBUG_LOG set to true?

    https://codex.wordpress.org/WP_DEBUG

  • Please try doing this

    
    add_filter('acf/settings/load_json', 'my_acf_json_load_point');
    function my_acf_json_load_point( $paths ) {
    // output paths and exit
    print_r($paths); die;
        error_log('hey!');
        
        // remove original path (optional)
        unset($paths[0]);
        
        // append path
        $paths[] = get_template_directory() . '/custom-fields';
        
        // return
        return $paths;
    }
    
  • Thank you, it doesn’t seem to be doing anything either though. Still nothing in the log 🙁

  • What I gave you should output the paths and stop and not continue to load any page on the site.

    The only explanation I have for this is that you are adding the filter after the hook has already run.

    You said that this is in your functions.php so I assumed it was added at the right time.

    However, it could be that something else is causing a premature initialization of ACF which can cause the hook to run before expected.

    You need to disable other plugins and maybe switch theme to narrow down what is causing the issue.

  • Yes! You were right. I have some other stuff in functions.php, so I tried putting your piece of code at the top of it and it worked. Then I narrowed it down by putting it after each function and test it to see when it actually broke. Turns out if I put it after my custom toolbar links, it doesn’t work. The hook looks like this (used this tutorial):

    add_action('admin_bar_menu', 'custom_toolbar_link', 999);

    I assume 999 is the priority, and it must be the source of the issue?

  • When I talk about “when” things happen it has to do with the order of hooks in WP and very little to do with the order of code.

    For example if you have these two filters added

    
    add_action('init', 'my_init_function');
    add_action('after_setup_theme', 'my_after_setup_theme_function');
    

    the function on the hook ‘after_setup_theme’ will always run before the function on the ‘init’ hook no matter what the priority is or what order these functions appear in the code.

    Here is the list of WP hooks and the order that they run https://codex.wordpress.org/Plugin_API/Action_Reference

    When I say that ACF might be prematurely initialized what I am looking for is any call to any ACF funtion that happens before the hook “after_theme_setup”. Filters and actions added in your functions.php file are not added until after this hook. This means that if something has initialized ACF before this time then you file and filters are loaded after acf/settings/load_json is run.

    things like this are usually caused by plugins because they are loaded before the theme files.

    If it does have something to do with add_action('admin_bar_menu', 'custom_toolbar_link', 999); then it would be in the code of the action function, not the adding of the action.

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

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.