1

I have the following I'm trying to enqueue in functions.php

wp_enqueue_script( 'param-example', 'https://domain.com/example?f=j&s=w&c=t', array(), null );

Except WordPress is escaping the ampersands in the HTML which is breaking the script.

How can I prevent WordPress from escaping the URL in wp_enqueue_script?

The other examples that seem close to this question haven't worked.

  • Are you sure that's what is breaking your script? I have an ampersand in mine and it's working fine. – rudtek Aug 16 '17 at 17:55
0

WordPress can automatically add the query variables for you. Instead of directly writing the query arguments, you can use it this way:

$args = array(
        'f' => 'j',
        's' => 'w',
        'c' => 't'
    );
wp_enqueue_script( 'param-example', add_query_arg( $args, 'https://domain.com/example') );

This is your solution, since according to code reference, the return value is unescaped by default.

  • This isn't what's needed. The variables don't need to be unescaped, the ampersands between the variables need to be. This doesn't do that. – Christian Norton May 5 '18 at 3:16
0

Another solution is to create a hook to target specific URLS.

e.g.

// 
// Add to functions.php, or to a Plugin file.
//
// Change, CHANGE_ME.com to Urls you want to stop wordpress from converting.
//
add_filter('clean_url', 'hook_strip_ampersand', 99, 3);
function hook_strip_ampersand($url, $original_url, $_context) {
    if (strstr($url, "CHANGE_ME.com") !== false) {
        $url = str_replace("&", "&", $url);
    }

    return $url;
}

For more information: https://stackoverflow.com/questions/9504142/loading-google-maps-api-with-wp-enqueue-script/9504653

  • How do you then enqueue the script? wp_enqueue_script( 'param-example', $url ); doesn't seem to do it. – Christian Norton May 5 '18 at 3:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.