admin管理员组文章数量:1289957
I have a script that I want to display in the wp_head, but only if a URL variable is passed. For example, if someone visits /?feedback=yes I want the following script to display:
<script>
alert( 'Hello, world!' );
</script>
I can already have this display all the time via the following function, but it's the part around having it only display if that URL variable exists that I'm not sure on.
/* Describe what the code snippet does so you can remember later on */
add_action('wp_head', 'your_function_name');
function your_function_name(){
?>
<script>
alert( 'Hello, world!' );
</script>
<?php
};
I have a script that I want to display in the wp_head, but only if a URL variable is passed. For example, if someone visits https://example/?feedback=yes I want the following script to display:
<script>
alert( 'Hello, world!' );
</script>
I can already have this display all the time via the following function, but it's the part around having it only display if that URL variable exists that I'm not sure on.
/* Describe what the code snippet does so you can remember later on */
add_action('wp_head', 'your_function_name');
function your_function_name(){
?>
<script>
alert( 'Hello, world!' );
</script>
<?php
};
Share
Improve this question
asked Jun 23, 2021 at 16:53
Moez TharaniMoez Tharani
1
1 Answer
Reset to default 0URL variables like that are stored in the $_GET
variable. You can check the existence and value of one like this:
add_action(
'wp_head',
function() {
if ( isset( $_GET['feedback'] ) && 'yes' === $_GET['feedback'] ) {
?>
<script>
alert( 'Hello, world!' );
</script>
<?php
}
}
);
The check for isset()
is necessary as checking $_GET['feedback']
alone will throw an error if it's not present in the URL at all.
本文标签: functionsDisplay Script in Header When URL Variable Present
版权声明:本文标题:functions - Display Script in Header When URL Variable Present 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741488469a2381512.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论