admin管理员组文章数量:1292620
I have the following I'm trying to enqueue in functions.php
wp_enqueue_script( 'param-example', ';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.
I have the following I'm trying to enqueue in functions.php
wp_enqueue_script( 'param-example', 'https://domain/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.
Share Improve this question edited Aug 17, 2017 at 11:06 Johansson 15.4k11 gold badges43 silver badges79 bronze badges asked Aug 16, 2017 at 17:26 Christian NortonChristian Norton 411 silver badge2 bronze badges 2- Are you sure that's what is breaking your script? I have an ampersand in mine and it's working fine. – rudtek Commented Aug 16, 2017 at 17:55
- Can you elaborate how is breaking the script when it's escaped? – Paul G. Commented May 12, 2021 at 22:15
2 Answers
Reset to default 1WordPress 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/example') );
This is your solution, since according to code reference, the return value is unescaped by default.
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 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") !== false) {
$url = str_replace("&", "&", $url);
}
return $url;
}
For more information: https://stackoverflow/questions/9504142/loading-google-maps-api-with-wp-enqueue-script/9504653
本文标签: query variableEnqueue Script with URL parameters
版权声明:本文标题:query variable - Enqueue Script with URL parameters 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741559403a2385358.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论