Skip to content

Commit a0b4b6a

Browse files
committed
Toolbar: apply default color scheme to front-end admin bar.
Reapply front-end admin bar color scheme support that was reverted in 9027fd9 because the previous implementation issued an HTTP request via wp_remote_get() on every front-end page load. Read the modern color scheme stylesheet directly from the local filesystem instead, and extract only rule blocks whose selector contains `#wpadminbar` so unrelated admin-only styles (pointers, media library, theme browser, customizer, etc.) are not leaked to the front end. Per-user color schemes are intentionally not honored to keep the cost predictable. See #64762.
1 parent 9027fd9 commit a0b4b6a

2 files changed

Lines changed: 25 additions & 0 deletions

File tree

src/wp-includes/admin-bar.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1462,3 +1462,27 @@ function _get_admin_bar_pref( $context = 'front', $user = 0 ) {
14621462

14631463
return 'true' === $pref;
14641464
}
1465+
1466+
/**
1467+
* Applies the default "modern" admin color scheme to the front-end admin bar.
1468+
*
1469+
* Per-user color schemes are not honored on the front end to avoid the
1470+
* per-request cost of resolving them.
1471+
*
1472+
* @since 7.0.0
1473+
*/
1474+
function wp_admin_bar_add_color_scheme_to_front_end() {
1475+
if ( is_admin() ) {
1476+
return;
1477+
}
1478+
1479+
$suffix = SCRIPT_DEBUG ? '' : '.min';
1480+
$css = @file_get_contents( ABSPATH . "wp-admin/css/colors/modern/colors$suffix.css" );
1481+
1482+
// Extract only rule blocks whose selector contains `#wpadminbar`.
1483+
if ( ! $css || ! preg_match_all( '/[^{}]*#wpadminbar[^{}]*\{[^{}]+\}/s', $css, $matches ) ) {
1484+
return;
1485+
}
1486+
1487+
wp_add_inline_style( 'admin-bar', implode( '', $matches[0] ) );
1488+
}

src/wp-includes/default-filters.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,7 @@
700700
add_action( 'wp_body_open', 'wp_admin_bar_render', 0 );
701701
add_action( 'wp_footer', 'wp_admin_bar_render', 1000 ); // Back-compat for themes not using `wp_body_open`.
702702
add_action( 'in_admin_header', 'wp_admin_bar_render', 0 );
703+
add_action( 'admin_bar_init', 'wp_admin_bar_add_color_scheme_to_front_end', 0 );
703704

704705
// Former admin filters that can also be hooked on the front end.
705706
add_action( 'media_buttons', 'media_buttons' );

0 commit comments

Comments
 (0)