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.

2 Answers
2

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.

Leave a Reply

Your email address will not be published. Required fields are marked *