Support

Account

Home Forums ACF PRO Multiple Save Locations for JSON? Reply To: Multiple Save Locations for JSON?

  • Sorry to dredge this up again; I wanted to share some code I wrote that may benefit others who find this question. I was looking for a solution a bit lighter than the suggested plugin and a bit more user-configurable than John’s (excellent) solution above, and came up with the following.

    I used a lot of PHP7+ syntax here—you may need to do a little translating if you’re running a lower version on your server!

    
    new acf_custom_json_save_path_setting();
    
    class acf_custom_json_save_path_setting {
        // This will store the preferred save path for the group for later retrieval.
        private $preferred_save_path;
    
        public function __construct() {
            // Add the "JSON Save Path" setting to all field groups.
            add_action('acf/render_field_group_settings', [$this, 'add_json_save_path_setting']);
    
            // Call an early bird (priority 1) action before saving the field group.
            add_action('acf/update_field_group', [$this, 'set_up_save_path'], 1, 1);
        }
    
        public function add_json_save_path_setting($field_group) {
            // Create our custom setting field with the specified options.
            acf_render_field_wrap([
                'label'        => 'JSON Save Path',
                'instructions' => 'Determines where the field group\'s JSON file will be saved, relative to the active theme\'s directory.',
                'type'         => 'text',
                'name'         => 'json_save_path',
                'prefix'       => 'acf_field_group',
                'prepend'      => '/',
                'placeholder'  => 'Use default path',
                'value'        => $field_group['json_save_path'] ?? '',
            ]);
        }
    
        public function set_up_save_path($group) {
            // Get the preferred save path, if set.
            $preferred_save_path = $group['json_save_path'] ?? null;
    
            // If not set (or set to an empty string), do nothing.
            if (!$preferred_save_path) {
                return $group;
            }
    
            // Set aside the preferred path and add an override action.
            $this->preferred_save_path = get_stylesheet_directory() . "/$preferred_save_path";
            add_action('acf/settings/save_json', [$this, 'save_to_preferred_save_path'], 9999);
    
            // Return the group for updating as usual.
            return $group;
        }
    
        public function save_to_preferred_save_path($path) {
            // Ensure this field group is saved to the preferred save path.
            $path = $this->preferred_save_path;
    
            return $path;
        }
    }
    

    The code above will add a simple “JSON Save Path” text field to each field group’s Settings section, in which you can enter a path where you’d like to save the group’s JSON file. The path is relative to the active theme’s directory (I felt like that was a safe default), but you can use standard directory traversal methods like ../ to go up a few levels if you’re, say, trying to save something to a plugin directory.

    And as a reminder, this code doesn’t do anything to load from your custom save points. I looked into it, but ensuring the array of load points always has the correct paths in it gets complicated quickly. Thankfully, it’s very simple (and much more transparent) to manage your load points manually via the acf/settings/load_json filter.