From af42621b09896298e8812aa8a81aa5ba071a0b17 Mon Sep 17 00:00:00 2001 From: Ben Leonard Date: Tue, 9 Sep 2025 14:23:30 +0100 Subject: [PATCH] Added code to correct WordPress core issue where font-size presets are not loaded correctly from theme.json --- includes/bug-fixes/all.php | 16 ++++ .../fix-wordpress-theme-json-font-sizes.php | 73 +++++++++++++++++++ plugin.php | 1 + theme-template/theme.json | 22 ++++++ 4 files changed, 112 insertions(+) create mode 100644 includes/bug-fixes/all.php create mode 100644 includes/bug-fixes/fix-wordpress-theme-json-font-sizes.php diff --git a/includes/bug-fixes/all.php b/includes/bug-fixes/all.php new file mode 100644 index 0000000..77c25a6 --- /dev/null +++ b/includes/bug-fixes/all.php @@ -0,0 +1,16 @@ +get_file_path( 'theme.json' ); + + // Ensure theme.json file exists and is readable. + if ( ! $theme_json_file || ! is_readable( $theme_json_file ) ) { + return $theme_json; + } + + // Parse theme.json file. + $theme_json_data = json_decode( file_get_contents( $theme_json_file ), true ); + + // Validate JSON parsing and structure. + if ( json_last_error() !== JSON_ERROR_NONE || ! is_array( $theme_json_data ) ) { + return $theme_json; + } + + // Extract font sizes from theme.json if they exist. + $theme_font_sizes = null; + + if ( isset( $theme_json_data['settings']['typography']['fontSizes'] ) && is_array( $theme_json_data['settings']['typography']['fontSizes'] ) && ! empty( $theme_json_data['settings']['typography']['fontSizes'] ) ) { + $theme_font_sizes = $theme_json_data['settings']['typography']['fontSizes']; + } + + // Only proceed if we found font sizes in theme.json. + if ( null === $theme_font_sizes ) { + return $theme_json; + } + + $data = $theme_json->get_data(); + + // Initialize settings structure if needed. + if ( ! isset( $data['settings'] ) ) { + $data['settings'] = array(); + } + if ( ! isset( $data['settings']['typography'] ) ) { + $data['settings']['typography'] = array(); + } + + // Apply font sizes from theme.json file. + $data['settings']['typography']['fontSizes'] = $theme_font_sizes; + + $theme_json->update_with( $data ); + + return $theme_json; + } +); diff --git a/plugin.php b/plugin.php index d430ba5..5867339 100644 --- a/plugin.php +++ b/plugin.php @@ -28,6 +28,7 @@ require_once __DIR__ . '/includes/commands/all.php'; require_once __DIR__ . '/includes/class-asset-enqueue.php'; require_once __DIR__ . '/includes/register-js-libraries.php'; +require_once __DIR__ . '/includes/bug-fixes/all.php'; // Register commands. Install_Theme_Command::register(); diff --git a/theme-template/theme.json b/theme-template/theme.json index 49dfde6..13a7685 100644 --- a/theme-template/theme.json +++ b/theme-template/theme.json @@ -37,6 +37,28 @@ "name": "Standard", "slug": "standard" } + ], + "fontSizes": [ + { + "name": "Small", + "slug": "small", + "size": "0.8125rem" + }, + { + "name": "Medium", + "slug": "medium", + "size": "1.25rem" + }, + { + "name": "Large", + "slug": "large", + "size": "2.25rem" + }, + { + "name": "Extra Large", + "slug": "x-large", + "size": "2.625rem" + } ] }, "shadow": {