Support

Account

Home Forums General Issues Problems with Local JSON

Solved

Problems with Local JSON

  • Hi there, it seems that after changes made in ACF 6.2, I have just discovered some problems with the Local JSON feature.

    In a plugin, I’m using the following code to save and read the JSON file in a specific folder:

    
    add_filter('acf/settings/save_json', 'my_acf_json_save_point');
     
    function my_acf_json_save_point( $path ) {
    
    	// update path
    	$path = '/path-to/wp-content/local-acf-json'; 
    	
    	// return
    	return $path;
    }
    
    add_filter('acf/settings/load_json', 'my_acf_json_load_point');
    
    function my_acf_json_load_point( $paths ) {
    
    	// remove original path (optional)
    	unset($paths[0]);
    
    	// append path
    	$paths[] = '/path-to/wp-content/local-acf-json';
    
    	// return
    	return $paths;
    }
    

    Up until recently, this worked great without problems. Now, after making a change to a field in the admin, I discovered that the change never happened on the edit page where the ACF field is. The ACF group shows that the field was saved.

    Digging further, I noticed that the JSON file is being saved in a different format as below:

    New:
    02_29_24_2b7b8a18647347e1b5f70053453b6cef.json

    Old:
    group_561aac73063f3.json

    After doing some testing, it seems that the new JSON field has the correct information, but for some reason it is being read from the old JSON file. I have also noticed that the JSON files are created new if the save happens on a different day.

    I did some reading in the DOCs and it shows the following code to change the JSON file name, though I did try this and it didn’t work for me, unfortunatley.

    function custom_acf_json_filename( $filename, $post, $load_path ) {
        $filename = str_replace(
            array(
                ' ',
                '_',
            ),
            array(
                '-',
                '-'
            ),
            $post['title']
        );
    
        $filename = strtolower( $filename ) . '.json';
    
        return $filename;
    }
    add_filter( 'acf/json/save_file_name', 'custom_acf_json_filename', 10, 3 );

    Any help on getting this back running properly would be very much appreciated.

    Thanks!

  • If anyone else comes across a similar issue, the problem was that ACF now runs the WordPress sanitize_file_name() function on the JSON file names before saving them. That being said, I was using a different function that uses sanitize_file_name() to rename image files.

    The fix was to write a condition into my existing function:

    function image_hash_rename( $filename ) {
    	$info = pathinfo( $filename );
    	$ext  = empty( $info['extension'] ) ? '' : '.' . $info['extension'];
    
    	if ( '.json' === $ext ) { // output ACF JSON files in correct format
    	return $filename;
    	}
    
    	$name = basename( $filename, $ext );    
    	$hash = md5( $name );
        return date('m_d_y') . '_' . $hash . $ext;
    }
    add_filter( 'sanitize_file_name', 'image_hash_rename', 10 );

    After doing so, the JSON files are now being saved in the correct format and I was able to sync with them again.

    Thanks to Matt Shaw (WP Engine Support) for working through this with me and create a solution that worked.

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

You must be logged in to reply to this topic.