@@ -70,6 +70,17 @@ public function _theme_root( $dir ) {
7070 return $ this ->theme_root ;
7171 }
7272
73+ /**
74+ * Registers an extra theme header for testing.
75+ *
76+ * @param string[] $headers Existing extra headers.
77+ * @return string[] Filtered extra headers.
78+ */
79+ public function filter_extra_theme_headers ( $ headers ) {
80+ $ headers [] = 'Custom Header ' ;
81+ return $ headers ;
82+ }
83+
7384 /**
7485 * Tests that theme.json metadata takes priority over style.css headers.
7586 *
@@ -152,6 +163,57 @@ public function test_block_theme_without_metadata_property_uses_stylesheet() {
152163 $ this ->assertFalse ( $ theme ->errors () );
153164 }
154165
166+ /**
167+ * Tests that custom theme headers registered via extra_theme_headers are read from theme.json metadata.
168+ *
169+ * @ticket 24152
170+ */
171+ public function test_theme_json_supports_extra_theme_headers () {
172+ $ theme_slug = 'json-custom-header-theme- ' . wp_generate_password ( 8 , false );
173+ $ theme_dir = $ this ->theme_root . '/ ' . $ theme_slug ;
174+
175+ add_filter ( 'extra_theme_headers ' , array ( $ this , 'filter_extra_theme_headers ' ) );
176+ mkdir ( $ theme_dir );
177+ try {
178+ file_put_contents (
179+ $ theme_dir . '/theme.json ' ,
180+ wp_json_encode (
181+ array (
182+ 'version ' => 3 ,
183+ 'metadata ' => array (
184+ 'name ' => 'JSON Custom Header Theme ' ,
185+ 'Custom Header ' => 'Custom Theme Header Value ' ,
186+ ),
187+ )
188+ )
189+ );
190+ file_put_contents ( $ theme_dir . '/style.css ' , "/* \nTheme Name: CSS Fallback \n*/ \n" );
191+ file_put_contents ( $ theme_dir . '/index.php ' , "<?php \n// Test theme. \n" );
192+
193+ wp_clean_themes_cache ();
194+ unset( $ GLOBALS ['wp_themes ' ] );
195+
196+ $ theme = new WP_Theme ( $ theme_slug , $ this ->theme_root );
197+ $ this ->assertSame ( 'Custom Theme Header Value ' , $ theme ->get ( 'Custom Header ' ) );
198+ } finally {
199+ remove_filter ( 'extra_theme_headers ' , array ( $ this , 'filter_extra_theme_headers ' ) );
200+ if ( file_exists ( $ theme_dir . '/theme.json ' ) ) {
201+ unlink ( $ theme_dir . '/theme.json ' );
202+ }
203+ if ( file_exists ( $ theme_dir . '/style.css ' ) ) {
204+ unlink ( $ theme_dir . '/style.css ' );
205+ }
206+ if ( file_exists ( $ theme_dir . '/index.php ' ) ) {
207+ unlink ( $ theme_dir . '/index.php ' );
208+ }
209+ if ( is_dir ( $ theme_dir ) ) {
210+ rmdir ( $ theme_dir );
211+ }
212+ wp_clean_themes_cache ();
213+ unset( $ GLOBALS ['wp_themes ' ] );
214+ }
215+ }
216+
155217 /**
156218 * Tests that active theme validation allows a JSON-metadata theme without style.css.
157219 *
0 commit comments