Make WordPress Core

Opened 18 months ago

Closed 18 months ago

Last modified 18 months ago

#59886 closed defect (bug) (wontfix)

enqueue scripts is html encoding the resulting string

Reported by: probed's profile probed Owned by:
Milestone: Priority: normal
Severity: normal Version: 6.4
Component: General Keywords: needs-patch
Focuses: Cc:

Description

<?php
add_filter('clean_url', function ($url) {
                if (strpos($url, 'spektrix-component-loader') === false) { // not our file
                    return $url;
                }
                return "$url' async data-components='spektrix-login-status,spektrix-donate,spektrix-merchandise,spektrix-memberships,spektrix-basket-summary,spektrix-gift-vouchers";
            }, 11, 1);
            wp_enqueue_script('webcomponents-spektrix', 'https://webcomponents.spektrix.com/stable/spektrix-component-loader.js');
            

causes things to be encoded in the url
this worked in prev versions
wp 6.4.1

Change History (5)

#1 @dmsnell
18 months ago

$url' async data-components='spektrix-login-status,spektrix-donate,spektrix-merchandise,spektrix-memberships,spektrix-basket-summary,spektrix-gift-vouchers

@probed what are you trying to accomplish here? it looks like you're attempting to add additional HTML attributes to the SCRIPT element by breaking out of the URL. is that the case?

if that's the case, I might recommend using the script_loader_tag filter instead.

<?php
add_filter( 'script_loader_tag', function ( $tag, $handle, $src ) {
        if ( 'webcomponents-spektrix' !== $handle ) {
                return $tag;
        }

        $processor = new WP_HTML_Tag_Processor( $tag );
        if ( $processor->next_tag( 'SCRIPT' ) ) {
                $processor->set_attribute( 'async', true );
                $processor->set_attribute( 'data-components', 'spektrix-login-status,spektrix-donate,spektrix-merchandise,spektrix-memberships,spektrix-basket-summary,spektrix-gift-vouchers' );
        }

        return $processor->get_updated_html();
}, 10, 3 );
Last edited 18 months ago by dmsnell (previous) (diff)

#2 @probed
18 months ago

ah, yes, that iw what I am attempting to do

it was a kind-of hacky fix to begin with, but at the time the only one i could find
I will update my code accordingly

Thank-you

#3 @dmsnell
18 months ago

Glad that was it @probed. Note that while it worked before, it shouldn't have 😅 because escaping out of the URL isn't something we want to do. The code I shared should work on WordPress 6.2 or newer. If you want to support older versions you'll need to modify the HTML at your own risk.

https://developer.wordpress.org/reference/classes/wp_html_tag_processor/

#4 @dmsnell
18 months ago

  • Resolution set to wontfix
  • Status changed from new to closed

#5 @TobiasBg
18 months ago

  • Milestone Awaiting Review deleted

Just for completion: An alternative could be the wp_script_attributes filter hook, available since WP 5.7.

Note: See TracTickets for help on using tickets.