Make WordPress Core

Changeset 62025


Ignore:
Timestamp:
03/14/2026 07:53:29 AM (5 months ago)
Author:
audrasjb
Message:

Toolbar: add CSS from admin color scheme on front-end.

This changeset introduces the wp_admin_bar_add_color_scheme_to_front_end() which is hooked on admin_bar_init in order to use the CSS from admin color schemes on the admin bar on front-end, as inline styles.

Props sabernhardt, huzaifaalmesbah, audrasjb, johnbillion, noruzzaman, JeffPaul, joedolson, huzaifaalmesbah, amesplant, r1k0, shailu25.
Fixes #64762.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/admin-bar.php

    r61979 r62025  
    14381438        return 'true' === $pref;
    14391439}
     1440
     1441/**
     1442 * Adds CSS from the administration color scheme stylesheet on the front end.
     1443 *
     1444 * @since 7.0.0
     1445 *
     1446 * @global array $_wp_admin_css_colors Registered administration color schemes.
     1447 */
     1448function wp_admin_bar_add_color_scheme_to_front_end() {
     1449        if ( is_admin() ) {
     1450                return;
     1451        }
     1452
     1453        global $_wp_admin_css_colors;
     1454
     1455        if ( empty( $_wp_admin_css_colors ) ) {
     1456                register_admin_color_schemes();
     1457        }
     1458
     1459        $color_scheme = get_user_option( 'admin_color' );
     1460
     1461        if ( empty( $color_scheme ) || ! isset( $_wp_admin_css_colors[ $color_scheme ] ) ) {
     1462                $color_scheme = 'modern';
     1463        }
     1464
     1465        $color = $_wp_admin_css_colors[ $color_scheme ] ?? null;
     1466        $url   = $color->url ?? '';
     1467
     1468        if ( $url ) {
     1469                $response = wp_remote_get( $url );
     1470                if ( ! is_wp_error( $response ) ) {
     1471                        $css = $response['body'];
     1472                        if ( is_string( $css ) && str_contains( $css, '#wpadminbar' ) ) {
     1473                                $start_position = strpos( $css, '#wpadminbar' );
     1474                                $end_position   = strpos( $css, '.wp-pointer' );
     1475                                if ( false !== $end_position && $end_position > $start_position ) {
     1476                                        $css = substr( $css, $start_position, $end_position - $start_position );
     1477                                        if ( SCRIPT_DEBUG ) {
     1478                                                $css = str_replace( '/* Pointers */', '', $css );
     1479                                        }
     1480                                }
     1481                                wp_add_inline_style( 'admin-bar', $css );
     1482                        }
     1483                }
     1484        }
     1485}
  • trunk/src/wp-includes/default-filters.php

    r61985 r62025  
    709709add_action( 'wp_footer', 'wp_admin_bar_render', 1000 ); // Back-compat for themes not using `wp_body_open`.
    710710add_action( 'in_admin_header', 'wp_admin_bar_render', 0 );
     711add_action( 'admin_bar_init', 'wp_admin_bar_add_color_scheme_to_front_end', 0 );
    711712
    712713// Former admin filters that can also be hooked on the front end.
  • trunk/tests/phpunit/tests/dependencies/wpStyleLoaderSrc.php

    r61388 r62025  
    1313        /**
    1414         * Tests that PHP warnings are not thrown when wp_style_loader_src() is called
    15          * before the `$_wp_admin_css_colors` global is set.
     15         * before the `$_wp_admin_css_colors` global is set within the admin.
    1616         *
    1717         * The warnings that we should not see:
     
    2020         *
    2121         * @ticket 61302
     22         * @ticket 64762
    2223         */
    2324        public function test_without_wp_admin_css_colors_global() {
    24                 $this->assertFalse( wp_style_loader_src( '', 'colors' ) );
     25                if ( is_admin() ) {
     26                        $this->assertFalse( wp_style_loader_src( '', 'colors' ) );
     27                } else {
     28                        $this->assertStringContainsString( '/colors.css', wp_style_loader_src( '', 'colors' ) );
     29                }
    2530        }
    2631}
Note: See TracChangeset for help on using the changeset viewer.