Opened 7 weeks ago
Last modified 7 weeks ago
#61937 new defect (bug)
WP_Theme_JSON_Schema::migrate removes data when version is missing
Reported by: | Soean | Owned by: | |
---|---|---|---|
Milestone: | Awaiting Review | Priority: | normal |
Severity: | normal | Version: | |
Component: | Themes | Keywords: | |
Focuses: | Cc: |
Description
I want to override a theme.json, therefore I can use the wp_theme_json_data_theme
filter.
I tried it with this code:
<?php add_filter( 'wp_theme_json_data_theme', function ( \WP_Theme_JSON_Data $theme_json_data ): \WP_Theme_JSON_Data { return $theme_json_data->update_with( [ // 'version' => 3, // only works with the version parameter 'settings' => [ 'color' => [ 'palette' => [ 'slug' => 'some-color', 'color' => '#01579b', 'name' => 'Some Color', ], ], ], ] ); } );
This only works when I add the version
, because the WP_Theme_JSON_Data
constructor calls WP_Theme_JSON_Schema::migrate()
.
I found out, that I need to add a version
to my array, otherwise this function overrides my data. See
- https://github.com/WordPress/wordpress-develop/blob/6.6.1/src/wp-includes/class-wp-theme-json-schema.php#L46-L50
- https://developer.wordpress.org/reference/classes/wp_theme_json_schema/migrate/
<?php if ( ! isset( $theme_json['version'] ) ) { $theme_json = array( 'version' => WP_Theme_JSON::LATEST_SCHEMA, ); } ...
We can fix it, if we only set the version parameter and don't override the array:
<?php if ( ! isset( $theme_json['version'] ) ) { $theme_json['version'] = WP_Theme_JSON::LATEST_SCHEMA; } ...
Note: See
TracTickets for help on using
tickets.
If we want a
version
, because otherwise we can't migrate old values, maybe we should call a_doing_it_wrong()
if the value is missing.