Support

Account

Home Forums ACF PRO Is it possible to have local JSON files in both theme and plugin folders

Solving

Is it possible to have local JSON files in both theme and plugin folders

  • I have ACF Pro installed as a plugin and I have created field groups that relate to my theme which are attached to page templates. When I make changes to the field groups the JSON files are being generated in my theme folder as expected.

    The site also requires some custom post types which I have create via plugin to keep separate from the theme. The fields for these custom post types are coming from new ACF field groups.

    I know its possible to change where local JSON files are stored but is there a way I can have JSON files for only the ACF field groups for the custom post types saved and loaded from the plugin folder and still have JSON files for the other ACF field groups relating to my theme save in the theme folder or can there only ever be a single folder for Lccal JSON files?

  • 
    <?php 
      
      new my_pluging_name_acf_group_save();
      
      class my_pluging_name_acf_group_save {
        
        // list of field group IDs used in my plugin
        private $groups = array(
          'group_5cd98d69d9edd',
          'group_5cdd8ef037a51',
          'group_5cdda2c94c13a',
          'group_5ce3e785e644e',
          'group_5d41e7d89ed6c'
        );
        
        public function __construct() {
          // add fitler before acf saves a group
          add_action('acf/update_field_group', array($this, 'update_field_group'), 1, 1);
        } // end public function __construct
        
        public function update_field_group($group) {
          // called when ACF save the field group to the DB
          if (in_array($group['key'], $this->groups)) {
            // if it is one of my groups then add a filter on the save location
            // high priority to make sure it is not overrridded, I hope
            add_filter('acf/settings/save_json',  array($this, 'override_location'), 9999);
          }
          return $group;
        } // end public function update_field_group
        
        public function override_location($path) {
          // remove this filter so it will not effect other goups
          remove_filter('acf/settings/save_json',  array($this, 'override_location'), 9999);
          // override save path
          $path = dirname(plugin_dir_path(__FILE__)).'/acf-json';
          return $path;
        } // end public function override_json_location
        
      } // end class my_pluging_name_acf_group_save
      
    

    There is also a plugin https://github.com/khromov/acf-local-json-manager

    and I’m sure there are other filters available

  • Should the above code go into my main plugin php file?

    I have tried adding it and updating a single group id to the array which seems to allow all the field groups to be saved into my-theme/acf-json apart from the group id listed which I believe is correct.

    The bit that doesn’t seem to be working is the group id added to the array that I want to be associated with my plugin isn’t being saved into the my-plugin/acf-json folder, it isn’t actually getting saved anywhere.

  • it can go in your plugin wherever you need it to go. You will need to adjust the path you want to save to based on how you set it up. You also need to make sure the folder where you want to save the json files exists on the server, ACF will not create the folder if it does not already exist and this will cause it to not save it at all.

  • I’ve managed to get it working using your code as a starting point. This is my first try at anything plugin related so there might be a better/cleaner way to do it, or things I haven’t considered.

    I have 2 ACF field groups

    • group_5d9700e0b69a7 – related to my theme and saves/loads/syncs from my-theme/acf-json folder
    • group_5d921d2978e9e – related to my plugin and saves/loads/syncs from my-plugin/acf-json folder
    /********************************/
    // Save and load plugin specific ACF field groups via the /acf-json folder.
    /********************************/
    
    // Save
    function my_plugin_update_field_group($group) {
      // list of field groups that should be saved to my-plugin/acf-json
      $groups = array('group_5d921d2978e9e');
    
      if (in_array($group['key'], $groups)) {
        add_filter('acf/settings/save_json', function() {
          return dirname(__FILE__) . '/acf-json';
        });
      }
    }
    add_action('acf/update_field_group', 'my_plugin_update_field_group', 1, 1);
    
    // Load - includes the /acf-json folder in this plugin to the places to look for ACF Local JSON files
    add_filter('acf/settings/load_json', function($paths) {
      $paths[] = dirname(__FILE__) . '/acf-json';
      return $paths;
    });
  • Works like a charm, thanks @steroyle.

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

The topic ‘Is it possible to have local JSON files in both theme and plugin folders’ is closed to new replies.