Support

Account

Home Forums General Issues Save individual field group automatically to plugin using save_json

Solved

Save individual field group automatically to plugin using save_json

  • Greetings,

    I have been working towards an ideal ACF + plugin + git workflow and have hit a roadblock. I have successfully loaded my json file from my plugin directory using ‘acf/settings/load_json’ but I would like to automatically save that particular field group BACK to the plugin instead of the acf-json folder in the theme.

    I would like to be able to keep the files in sync via git but avoid filling up the theme with json files that don’t belong there. My thought was to hook into save_post and check if it is the relevant field group and then add my own acf/settings/save_json filter and remove it when done.

    Has anyone done anything like this? I know I can set a field group to private which is cool once my field groups are locked down but it would be good to have some kind of plugin based json storage workflow.

    Regards,
    Michael

  • Hi Michael,

    So there would be two things you need to consider. First is to save your fieldgroup JSON in your own location and secondly to tell ACF to also look in your location for JSON files.

    For the second part there’s the function you already seem to have in place so that’s good 🙂

    As for the saving part there is only one save point.. I think in theory you could maybe do what you’re asking. Create your own save_json filter on save_post but that might be too late as well..

    I’m not sure wether ACF checks the save path on each save tho or just go fetch the save path directly from the DB.. if it does check each time you might even be able to do a check for the $_POST variables inside the function and figure out if you’re on the specific field group and change the path accordingly.

    
    add_filter('acf/settings/save_json', 'my_acf_json_save_point');
     
    function my_acf_json_save_point( $path ) {
        
    //If you have wp_debug active this will put the entire $_POST in your debug.log file in wp-content. 
    error_log(print_r($_POST, true));
    
    if( $_POST['somethingsomething'] )
        $path = get_stylesheet_directory() . '/my-custom-folder';
        
        
        // return
        return $path;
        
    }
    
  • This looks promising, checking the $_POST could work well, thanks.

    I’ll give it a go and let you know how I go.

  • Here is what I come up with based on your suggestion:

    
        function my_save_json( $path ) {
    
            if( isset($_POST['acf_field_group']['key']) ) {
    
                $paths = my_load_json(array()); //  pass in empty array to only get local paths
    
                foreach( $paths as $load_path ) {
    
                    //  loop over each load path for json files
                    foreach (glob($load_path . "/*.json") as $filename) {
                        
                        $field_group = json_decode(file_get_contents($filename), true);
                        
                        //  if the key matches a group in this plugin then return the path
                        if (is_array($field_group) && $field_group['key'] == $_POST['acf_field_group']['key']) {
                            return $load_path;
                        }
                    }
    
                }
    
            }
    
            // return
            return $path;
    
        }
    
        function my_load_json( $paths ) {
            $paths[] = MY_PLUGIN_DIR .'fields';
            return $paths;
        }
    
    

    This works on next field group save if I move the generated JSON file into my plugin directory.

    How does that look to you?

  • Do that!

    I’ll keep my fingers crossed that ACF does a check for the path with each save.

  • Hi @mbryne

    Great 🙂
    If it works I’m sure it’s fine. Altho if you already know the fieldgroups name/key and you’ll only include one (or a specific amount) couldn’t you make your life easier by just checking for those specific values in $_POST and change the path accordingly? It’s not as flexible but helluva lot cleaner, faster and safer code (less chance of screwup) 🙂

  • This will be spread across multiple plugins and field groups over time and its really about keeping that workflow as simple as possible so I’m happy with the performance hit for ease of use and that flexibility.

    It’s a weird use case really, I am just after that holy grail. Im after portability between devs / environments, storing JSON where its theme independent and alongside its associated plugin functionality.

    Thanks for your help.

  • Ah okay 🙂

    Yeah I’m sure this is fine! Who isn’t always after the hole grail! My workflow with plugins is usually when a plugin relies heavily on ACF I see it as a site-specific plugin and tend to not try to over-modulate everything. But if it’s a plugin I know I’ll want to use for many sites or require so little custom meta I tend to code it myself..

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

The topic ‘Save individual field group automatically to plugin using save_json’ is closed to new replies.