Problems with theme.json on Sage 9 after WP update to 6.3

I have few old sites that we have with Sage 9, and after update to WordPress 6.3 all the settings and parameter in theme.json has disappear from the editor.

Did you know if there is any hook to force to load correctly theme.json in this case?
I’ve been reading some topics related but no one gave me a possible way, and now I have a problem with 2 of these sites that use colors and spacing a lot.

Someone had same problems?

Thanks in advance!

After some research, digging into the WordPress code in one of the classes it uses for the theme.json I managed to find and use the wp_theme_json_data_theme hook that allows you to update the data passed to WP from the theme’s own json file.

Maybe not the perfect solution, but it works as a workaround, here is the code if anyone needs it:

add_filter( 'wp_theme_json_data_theme', function( $theme_json ){
    // Set path to theme.json file
    $json_file = get_stylesheet_directory() . '/theme.json';

    // Check if file exists
    if( !file_exists( $json_file ) ) {
        return $theme_json;
    }

    // Load theme.json file from theme root in $new_data
    $new_data = json_decode( file_get_contents( $json_file ), true );

	return $theme_json->update_with( $new_data );
} );

If you know a better solution, let me know :slight_smile:

1 Like

The issue I’m experiencing is when the wp_get_global_stylesheet() function is called in global-styles-and-settings.php, I receive an error despite having the theme.json file in the correct location of the theme:

Notice: Error when decoding a JSON file at path /app/web/app/themes/wordpress-theme/resources: Syntax error in /app/web/wp/wp-includes/functions.php on line 4578

This is because when it calls get_theme_data() in class-wp-theme-json-resolver.php it is setting the $theme_json_file to the directory, with the file name. This is because the get_file_path method of WP_Theme has a filter that applies the dirname filter.

And the is_readable() function evaluates to true because the directory exists. Then the JSON decode fails, resulting in the above notice. I don’t understand why file_exists wasn’t used instead.

Does anyone know of a way to adjust the theme so that this notice doesn’t occur, filling log files?

Yeah this is weird one!
Having the same error indeed on Sage 9 themes after updating to WP 6.3 and I am not even using a theme.json file in the resources folder?

There’s some additional discussion here: Sage 9 + Wordpress 6.3 : Notice: Error when decoding a JSON file at path... · Issue #3143 · roots/sage · GitHub

Adding this snippet to filters.php was the solution that worked best for me on an old project I updated to latest WP core, etc…

add_filter('theme_file_path', function($path, $file) {
    if($file === 'theme.json') {
        return false;
    }
    return $path;
}, 0, 2);
11 Likes