Make WordPress Core

Opened 2 years ago

Closed 2 years ago

Last modified 2 years ago

#55539 closed defect (bug) (fixed)

add_menu_page no longer renders base64 encoded SVG icon

Reported by: kprovance's profile KProvance Owned by: sergeybiryukov's profile SergeyBiryukov
Milestone: 6.0 Priority: normal
Severity: normal Version: 6.0
Component: Administration Keywords: has-patch commit
Focuses: administration Cc:

Description

Regarding the add_menu_page() API.

Passing a base64 encoded string (per the instruction provided on the codex for this API (Pass a base64-encoded SVG using a data URI, which will be colored to match the color scheme. This should begin with 'data:image/svg+xml;base64,')) in the $icon_url parameter no longer outputs the icon. The base64 string no longer outputs in the HTML.

The feature still functions properly in WP 5.9.x. As WooCommerce uses this method for their primary admin menu icon, one can see it reproduced by installing and activating WC.

Occurs with FF and Chrome. Using PHP 7.4 and 8.1 alternatively.

Change History (10)

#1 @w33zy
2 years ago

I am also experience this issue this and I've tracked it down to esc_url() function on L443.

However, I am not experienced enough with Trac or SVN to pinpoint what was changed and when it was changed.

#2 @kebbet
2 years ago

  • Focuses administration added
  • Keywords needs-patch added
  • Version set to trunk

Yes, can confirm this. Works in 5.9.3, not in current trunk.

Tested with this code.

<?php
function register_menu_with_custom_svg_icon() {

        //The icon in Base64 format
        $icon_base64 = 'PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz4KPCEtLSBHZW5lcmF0b3I6IEFkb2JlIElsbHVzdHJhdG9yIDI2LjEuMCwgU1ZHIEV4cG9ydCBQbHVnLUluIC4gU1ZHIFZlcnNpb246IDYuMDAgQnVpbGQgMCkgIC0tPgo8c3ZnIHZlcnNpb249IjEuMSIgaWQ9IkxhZ2VyXzEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgeG1sbnM6eGxpbms9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkveGxpbmsiIHg9IjBweCIgeT0iMHB4IgoJIHZpZXdCb3g9IjAgMCAyMCAyMCIgc3R5bGU9ImVuYWJsZS1iYWNrZ3JvdW5kOm5ldyAwIDAgMjAgMjA7IiB4bWw6c3BhY2U9InByZXNlcnZlIj4KPHN0eWxlIHR5cGU9InRleHQvY3NzIj4KCS5zdDB7c3Ryb2tlOiNFMDEwMTA7c3Ryb2tlLXdpZHRoOjI7fQo8L3N0eWxlPgo8cGF0aCBjbGFzcz0ic3QwIiBkPSJNMCwyaDIwIE0wLDEwaDIwIE0wLDE4aDIwIi8+Cjwvc3ZnPgo=';
        
        //The icon in the data URI scheme
        $icon_data_uri = 'data:image/svg+xml;base64,' . $icon_base64;
        
        add_menu_page(
        'Example',
        'Example',
        'manage_options',
        'example',
        '',
        $icon_data_uri,
        );
        
}
        
add_action( 'admin_menu', 'register_menu_with_custom_svg_icon' );

#3 @ocean90
2 years ago

  • Milestone changed from Awaiting Review to 6.0

This was broken by the fix for #55496 in [53050].

#4 @kebbet
2 years ago

Changing line 132 in wp-admin/menu-header.php to

$img_style = ' style="background-image:url(\'' . esc_attr( $item[6] ) . '\')"';

fixes the issue. I can make a PR for this at GitHub.

This ticket was mentioned in PR #2528 on WordPress/wordpress-develop by kebbet.


2 years ago
#5

  • Keywords has-patch added; needs-patch removed

#6 @mukesh27
2 years ago

  • Keywords commit added
  • Owner set to SergeyBiryukov
  • Status changed from new to assigned

Hi there!

Thank you for the ticket and patch.

PR looks fine to me.

Testing for Trunk version

  1. Add the below code in theme function.php
<?php
function register_menu_with_custom_svg_icon() {

        //The icon in Base64 format
        $icon_base64 = 'PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz4KPCEtLSBHZW5lcmF0b3I6IEFkb2JlIElsbHVzdHJhdG9yIDI2LjEuMCwgU1ZHIEV4cG9ydCBQbHVnLUluIC4gU1ZHIFZlcnNpb246IDYuMDAgQnVpbGQgMCkgIC0tPgo8c3ZnIHZlcnNpb249IjEuMSIgaWQ9IkxhZ2VyXzEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgeG1sbnM6eGxpbms9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkveGxpbmsiIHg9IjBweCIgeT0iMHB4IgoJIHZpZXdCb3g9IjAgMCAyMCAyMCIgc3R5bGU9ImVuYWJsZS1iYWNrZ3JvdW5kOm5ldyAwIDAgMjAgMjA7IiB4bWw6c3BhY2U9InByZXNlcnZlIj4KPHN0eWxlIHR5cGU9InRleHQvY3NzIj4KCS5zdDB7c3Ryb2tlOiNFMDEwMTA7c3Ryb2tlLXdpZHRoOjI7fQo8L3N0eWxlPgo8cGF0aCBjbGFzcz0ic3QwIiBkPSJNMCwyaDIwIE0wLDEwaDIwIE0wLDE4aDIwIi8+Cjwvc3ZnPgo=';
        
        //The icon in the data URI scheme
        $icon_data_uri = 'data:image/svg+xml;base64,' . $icon_base64;
        
        add_menu_page(
        'Example',
        'Example',
        'manage_options',
        'example',
        '',
        $icon_data_uri,
        );
        
}
        
add_action( 'admin_menu', 'register_menu_with_custom_svg_icon' );
  1. Icon is not visible for menu item.
  2. Apply PR
  3. Icon shows fine.

Note: We have to add comment so other committer or contributor do not change it again.

#7 follow-up: @SergeyBiryukov
2 years ago

Hi there, welcome to WordPress Trac! Good catch, thanks for the ticket.

#8 @SergeyBiryukov
2 years ago

  • Resolution set to fixed
  • Status changed from assigned to closed

In 53092:

Administration: Restore the correct escaping function for base64-encoded SVG icons in the admin menu.

Add a comment to clarify the esc_attr() usage instead of esc_url().

Follow-up to [53050].

Props KProvance, kebbet, w33zy, ocean90, mukesh27.
Fixes #55539.

#10 in reply to: ↑ 7 @KProvance
2 years ago

Replying to SergeyBiryukov:

Hi there, welcome to WordPress Trac! Good catch, thanks for the ticket.

Not a problem. Glad I could help. And props to everyone who fixed it. Well done, folks! :)

Note: See TracTickets for help on using tickets.