From 5389ebdfd570d87ad9cb9c0d5ff7aea55fc3d25e Mon Sep 17 00:00:00 2001 From: "Stephen A. Bernhardt" Date: Fri, 6 Mar 2026 07:33:33 -0600 Subject: [PATCH 01/10] Add new function in `admin-bar.php` --- src/wp-includes/admin-bar.php | 43 +++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/wp-includes/admin-bar.php b/src/wp-includes/admin-bar.php index 9fc3c2b46b348..d5cc0eaa659df 100644 --- a/src/wp-includes/admin-bar.php +++ b/src/wp-includes/admin-bar.php @@ -1399,3 +1399,46 @@ function _get_admin_bar_pref( $context = 'front', $user = 0 ) { return 'true' === $pref; } + +/** + * Adds CSS from the administration color scheme stylesheet on the front end. + * + * @since 7.0.0 + * + * @global array $_wp_admin_css_colors Registered administration color schemes. + */ +function wp_admin_bar_add_color_scheme_to_front_end() { + global $_wp_admin_css_colors; + + if ( is_admin() ) { + return; + } + + if ( empty( $_wp_admin_css_colors ) ) { + register_admin_color_schemes(); + } + + $color_scheme = get_user_option( 'admin_color' ); + + if ( empty( $color_scheme ) || ! isset( $_wp_admin_css_colors[ $color_scheme ] ) ) { + $color_scheme = 'modern'; + } + + $color = $_wp_admin_css_colors[ $color_scheme ] ?? null; + $url = $color->url ?? ''; + + if ( $url ) { + $css = file_get_contents( $url ); + if ( is_string( $css ) && str_contains( $css, '#wpadminbar' ) ) { + $start_position = strpos( $css, '#wpadminbar' ); + $end_position = strpos( $css, '.wp-pointer' ); + if ( false !== $end_position && $end_position > $start_position ) { + $css = substr( $css, $start_position, $end_position - $start_position ); + if ( SCRIPT_DEBUG ) { + $css = str_replace( '/* Pointers */', '', $css ); + } + } + wp_add_inline_style( 'admin-bar', $css ); + } + } +} From 0f09fc9343ddd225dc73076793e850b47729d767 Mon Sep 17 00:00:00 2001 From: "Stephen A. Bernhardt" Date: Fri, 6 Mar 2026 07:35:03 -0600 Subject: [PATCH 02/10] Add function to `admin_bar_init` action --- src/wp-includes/default-filters.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/wp-includes/default-filters.php b/src/wp-includes/default-filters.php index 0bcd2d6b15acb..021ee08118b27 100644 --- a/src/wp-includes/default-filters.php +++ b/src/wp-includes/default-filters.php @@ -705,6 +705,7 @@ add_action( 'wp_body_open', 'wp_admin_bar_render', 0 ); add_action( 'wp_footer', 'wp_admin_bar_render', 1000 ); // Back-compat for themes not using `wp_body_open`. add_action( 'in_admin_header', 'wp_admin_bar_render', 0 ); +add_action( 'admin_bar_init', 'wp_admin_bar_add_color_scheme_to_front_end', 0 ); // Former admin filters that can also be hooked on the front end. add_action( 'media_buttons', 'media_buttons' ); From bd7f13639d4b4401a154c88efc59d4e0d3e71735 Mon Sep 17 00:00:00 2001 From: "Stephen A. Bernhardt" Date: Fri, 6 Mar 2026 08:26:32 -0600 Subject: [PATCH 03/10] Edit unit test --- tests/phpunit/tests/dependencies/wpStyleLoaderSrc.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/tests/phpunit/tests/dependencies/wpStyleLoaderSrc.php b/tests/phpunit/tests/dependencies/wpStyleLoaderSrc.php index 6155ab0d178c3..58c2a8111ea39 100644 --- a/tests/phpunit/tests/dependencies/wpStyleLoaderSrc.php +++ b/tests/phpunit/tests/dependencies/wpStyleLoaderSrc.php @@ -12,15 +12,20 @@ class Tests_Dependencies_wpStyleLoaderSrc extends WP_UnitTestCase { /** * Tests that PHP warnings are not thrown when wp_style_loader_src() is called - * before the `$_wp_admin_css_colors` global is set. + * before the `$_wp_admin_css_colors` global is set within the admin. * * The warnings that we should not see: * `Warning: Trying to access array offset on null`. * `Warning: Attempt to read property "url" on null`. * * @ticket 61302 + * @ticket 64762 */ public function test_without_wp_admin_css_colors_global() { - $this->assertFalse( wp_style_loader_src( '', 'colors' ) ); + if ( is_admin() ) { + $this->assertFalse( wp_style_loader_src( '', 'colors' ) ); + } else { + $this->assertSame( 'http://example.org/wp-admin/css/colors/modern/colors.css', wp_style_loader_src( '', 'colors' ) ); + } } } From 9c2197478f17eec8b6eb89b8ad2ba8e235d7c4c0 Mon Sep 17 00:00:00 2001 From: "Stephen A. Bernhardt" Date: Fri, 6 Mar 2026 08:53:39 -0600 Subject: [PATCH 04/10] Check if file is readable --- src/wp-includes/admin-bar.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/admin-bar.php b/src/wp-includes/admin-bar.php index d5cc0eaa659df..06e9fec178579 100644 --- a/src/wp-includes/admin-bar.php +++ b/src/wp-includes/admin-bar.php @@ -1427,7 +1427,7 @@ function wp_admin_bar_add_color_scheme_to_front_end() { $color = $_wp_admin_css_colors[ $color_scheme ] ?? null; $url = $color->url ?? ''; - if ( $url ) { + if ( $url && is_readable( $url ) ) { $css = file_get_contents( $url ); if ( is_string( $css ) && str_contains( $css, '#wpadminbar' ) ) { $start_position = strpos( $css, '#wpadminbar' ); From 26e114f260e11f980ae50749c49ac32f554c4c1f Mon Sep 17 00:00:00 2001 From: "Stephen A. Bernhardt" Date: Fri, 6 Mar 2026 09:07:30 -0600 Subject: [PATCH 05/10] Move global variable down, remove `is_readable` --- src/wp-includes/admin-bar.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/admin-bar.php b/src/wp-includes/admin-bar.php index 06e9fec178579..56447ae517898 100644 --- a/src/wp-includes/admin-bar.php +++ b/src/wp-includes/admin-bar.php @@ -1408,12 +1408,12 @@ function _get_admin_bar_pref( $context = 'front', $user = 0 ) { * @global array $_wp_admin_css_colors Registered administration color schemes. */ function wp_admin_bar_add_color_scheme_to_front_end() { - global $_wp_admin_css_colors; - if ( is_admin() ) { return; } + global $_wp_admin_css_colors; + if ( empty( $_wp_admin_css_colors ) ) { register_admin_color_schemes(); } @@ -1427,7 +1427,7 @@ function wp_admin_bar_add_color_scheme_to_front_end() { $color = $_wp_admin_css_colors[ $color_scheme ] ?? null; $url = $color->url ?? ''; - if ( $url && is_readable( $url ) ) { + if ( $url ) { $css = file_get_contents( $url ); if ( is_string( $css ) && str_contains( $css, '#wpadminbar' ) ) { $start_position = strpos( $css, '#wpadminbar' ); From ed9e3d0ff24d1a44f5f9c55f7519662e358a93d5 Mon Sep 17 00:00:00 2001 From: "Stephen A. Bernhardt" Date: Fri, 6 Mar 2026 09:28:00 -0600 Subject: [PATCH 06/10] Revert "Edit unit test" This reverts commit bd7f13639d4b4401a154c88efc59d4e0d3e71735. --- tests/phpunit/tests/dependencies/wpStyleLoaderSrc.php | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/tests/phpunit/tests/dependencies/wpStyleLoaderSrc.php b/tests/phpunit/tests/dependencies/wpStyleLoaderSrc.php index 58c2a8111ea39..6155ab0d178c3 100644 --- a/tests/phpunit/tests/dependencies/wpStyleLoaderSrc.php +++ b/tests/phpunit/tests/dependencies/wpStyleLoaderSrc.php @@ -12,20 +12,15 @@ class Tests_Dependencies_wpStyleLoaderSrc extends WP_UnitTestCase { /** * Tests that PHP warnings are not thrown when wp_style_loader_src() is called - * before the `$_wp_admin_css_colors` global is set within the admin. + * before the `$_wp_admin_css_colors` global is set. * * The warnings that we should not see: * `Warning: Trying to access array offset on null`. * `Warning: Attempt to read property "url" on null`. * * @ticket 61302 - * @ticket 64762 */ public function test_without_wp_admin_css_colors_global() { - if ( is_admin() ) { - $this->assertFalse( wp_style_loader_src( '', 'colors' ) ); - } else { - $this->assertSame( 'http://example.org/wp-admin/css/colors/modern/colors.css', wp_style_loader_src( '', 'colors' ) ); - } + $this->assertFalse( wp_style_loader_src( '', 'colors' ) ); } } From 3d48cc118f5462c20b5a5b75dff87a803f0665ec Mon Sep 17 00:00:00 2001 From: "Stephen A. Bernhardt" Date: Fri, 6 Mar 2026 10:20:12 -0600 Subject: [PATCH 07/10] Reapply "Edit unit test" This reverts commit ed9e3d0ff24d1a44f5f9c55f7519662e358a93d5. --- tests/phpunit/tests/dependencies/wpStyleLoaderSrc.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/tests/phpunit/tests/dependencies/wpStyleLoaderSrc.php b/tests/phpunit/tests/dependencies/wpStyleLoaderSrc.php index 6155ab0d178c3..58c2a8111ea39 100644 --- a/tests/phpunit/tests/dependencies/wpStyleLoaderSrc.php +++ b/tests/phpunit/tests/dependencies/wpStyleLoaderSrc.php @@ -12,15 +12,20 @@ class Tests_Dependencies_wpStyleLoaderSrc extends WP_UnitTestCase { /** * Tests that PHP warnings are not thrown when wp_style_loader_src() is called - * before the `$_wp_admin_css_colors` global is set. + * before the `$_wp_admin_css_colors` global is set within the admin. * * The warnings that we should not see: * `Warning: Trying to access array offset on null`. * `Warning: Attempt to read property "url" on null`. * * @ticket 61302 + * @ticket 64762 */ public function test_without_wp_admin_css_colors_global() { - $this->assertFalse( wp_style_loader_src( '', 'colors' ) ); + if ( is_admin() ) { + $this->assertFalse( wp_style_loader_src( '', 'colors' ) ); + } else { + $this->assertSame( 'http://example.org/wp-admin/css/colors/modern/colors.css', wp_style_loader_src( '', 'colors' ) ); + } } } From 7a1246db6e6bef59f8f549f1cadc13630d948dc5 Mon Sep 17 00:00:00 2001 From: "Stephen A. Bernhardt" Date: Fri, 6 Mar 2026 10:23:31 -0600 Subject: [PATCH 08/10] Replace `file_get_contents` with `wp_remote_get` --- src/wp-includes/admin-bar.php | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/wp-includes/admin-bar.php b/src/wp-includes/admin-bar.php index 56447ae517898..2186802d5feda 100644 --- a/src/wp-includes/admin-bar.php +++ b/src/wp-includes/admin-bar.php @@ -1428,17 +1428,20 @@ function wp_admin_bar_add_color_scheme_to_front_end() { $url = $color->url ?? ''; if ( $url ) { - $css = file_get_contents( $url ); - if ( is_string( $css ) && str_contains( $css, '#wpadminbar' ) ) { - $start_position = strpos( $css, '#wpadminbar' ); - $end_position = strpos( $css, '.wp-pointer' ); - if ( false !== $end_position && $end_position > $start_position ) { - $css = substr( $css, $start_position, $end_position - $start_position ); - if ( SCRIPT_DEBUG ) { - $css = str_replace( '/* Pointers */', '', $css ); + $response = wp_remote_get( $url ); + if ( ! is_wp_error( $response ) ) { + $css = $response['body']; + if ( is_string( $css ) && str_contains( $css, '#wpadminbar' ) ) { + $start_position = strpos( $css, '#wpadminbar' ); + $end_position = strpos( $css, '.wp-pointer' ); + if ( false !== $end_position && $end_position > $start_position ) { + $css = substr( $css, $start_position, $end_position - $start_position ); + if ( SCRIPT_DEBUG ) { + $css = str_replace( '/* Pointers */', '', $css ); + } } + wp_add_inline_style( 'admin-bar', $css ); } - wp_add_inline_style( 'admin-bar', $css ); } } } From 3038d991fd7d7fd8d8706f8bab6e933ea34de220 Mon Sep 17 00:00:00 2001 From: "Stephen A. Bernhardt" Date: Sat, 7 Mar 2026 21:37:35 -0600 Subject: [PATCH 09/10] Edit unit test to check for part of URL, not full --- tests/phpunit/tests/dependencies/wpStyleLoaderSrc.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit/tests/dependencies/wpStyleLoaderSrc.php b/tests/phpunit/tests/dependencies/wpStyleLoaderSrc.php index 58c2a8111ea39..404ace28c9f4f 100644 --- a/tests/phpunit/tests/dependencies/wpStyleLoaderSrc.php +++ b/tests/phpunit/tests/dependencies/wpStyleLoaderSrc.php @@ -25,7 +25,7 @@ public function test_without_wp_admin_css_colors_global() { if ( is_admin() ) { $this->assertFalse( wp_style_loader_src( '', 'colors' ) ); } else { - $this->assertSame( 'http://example.org/wp-admin/css/colors/modern/colors.css', wp_style_loader_src( '', 'colors' ) ); + $this->assertContains( '/colors.css', wp_style_loader_src( '', 'colors' ) ); } } } From 066516845bc4be0ba17277d952005f14d2d9e27f Mon Sep 17 00:00:00 2001 From: "Stephen A. Bernhardt" Date: Sat, 7 Mar 2026 23:49:34 -0600 Subject: [PATCH 10/10] `assertStringContainsString` --- tests/phpunit/tests/dependencies/wpStyleLoaderSrc.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit/tests/dependencies/wpStyleLoaderSrc.php b/tests/phpunit/tests/dependencies/wpStyleLoaderSrc.php index 404ace28c9f4f..170129580a3e7 100644 --- a/tests/phpunit/tests/dependencies/wpStyleLoaderSrc.php +++ b/tests/phpunit/tests/dependencies/wpStyleLoaderSrc.php @@ -25,7 +25,7 @@ public function test_without_wp_admin_css_colors_global() { if ( is_admin() ) { $this->assertFalse( wp_style_loader_src( '', 'colors' ) ); } else { - $this->assertContains( '/colors.css', wp_style_loader_src( '', 'colors' ) ); + $this->assertStringContainsString( '/colors.css', wp_style_loader_src( '', 'colors' ) ); } } }