|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * WordPress Theme.json Font Size Bug Fix |
| 4 | + * |
| 5 | + * Workaround for WordPress 6.8.2 bug where theme.json font size presets |
| 6 | + * are not properly loaded. Intercepts default font size processing and |
| 7 | + * applies theme's intended font size values. |
| 8 | + * |
| 9 | + * @package Creode Theme |
| 10 | + */ |
| 11 | + |
| 12 | +// Prevent direct access. |
| 13 | +if ( ! defined( 'ABSPATH' ) ) { |
| 14 | + exit; |
| 15 | +} |
| 16 | + |
| 17 | +/** |
| 18 | + * Fix WordPress 6.8.2 theme.json font size loading bug. |
| 19 | + * |
| 20 | + * Loads theme.json file directly and extracts font sizes to ensure they |
| 21 | + * are properly applied. Uses wp_theme_json_data_default filter with high |
| 22 | + * priority because wp_theme_json_data_theme filter is not working in WordPress 6.8.2. |
| 23 | + */ |
| 24 | +add_filter( |
| 25 | + 'wp_theme_json_data_default', |
| 26 | + function ( WP_Theme_JSON_Data $theme_json ) { |
| 27 | + // Load theme.json file directly using WordPress API. |
| 28 | + $wp_theme = wp_get_theme(); |
| 29 | + $theme_json_file = $wp_theme->get_file_path( 'theme.json' ); |
| 30 | + |
| 31 | + // Ensure theme.json file exists and is readable. |
| 32 | + if ( ! $theme_json_file || ! is_readable( $theme_json_file ) ) { |
| 33 | + return $theme_json; |
| 34 | + } |
| 35 | + |
| 36 | + // Parse theme.json file. |
| 37 | + $theme_json_data = json_decode( file_get_contents( $theme_json_file ), true ); |
| 38 | + |
| 39 | + // Validate JSON parsing and structure. |
| 40 | + if ( json_last_error() !== JSON_ERROR_NONE || ! is_array( $theme_json_data ) ) { |
| 41 | + return $theme_json; |
| 42 | + } |
| 43 | + |
| 44 | + // Extract font sizes from theme.json if they exist. |
| 45 | + $theme_font_sizes = null; |
| 46 | + |
| 47 | + if ( isset( $theme_json_data['settings']['typography']['fontSizes'] ) && is_array( $theme_json_data['settings']['typography']['fontSizes'] ) && ! empty( $theme_json_data['settings']['typography']['fontSizes'] ) ) { |
| 48 | + $theme_font_sizes = $theme_json_data['settings']['typography']['fontSizes']; |
| 49 | + } |
| 50 | + |
| 51 | + // Only proceed if we found font sizes in theme.json. |
| 52 | + if ( null === $theme_font_sizes ) { |
| 53 | + return $theme_json; |
| 54 | + } |
| 55 | + |
| 56 | + $data = $theme_json->get_data(); |
| 57 | + |
| 58 | + // Initialize settings structure if needed. |
| 59 | + if ( ! isset( $data['settings'] ) ) { |
| 60 | + $data['settings'] = array(); |
| 61 | + } |
| 62 | + if ( ! isset( $data['settings']['typography'] ) ) { |
| 63 | + $data['settings']['typography'] = array(); |
| 64 | + } |
| 65 | + |
| 66 | + // Apply font sizes from theme.json file. |
| 67 | + $data['settings']['typography']['fontSizes'] = $theme_font_sizes; |
| 68 | + |
| 69 | + $theme_json->update_with( $data ); |
| 70 | + |
| 71 | + return $theme_json; |
| 72 | + } |
| 73 | +); |
0 commit comments