I need to pass a variable inside the permalink that takes me to my custom post, and then execute the $_GET
of this variable inside an input of my cf7.
The only problem Wordpress encodes the &
in #038;
I tried to decode the special characters but nothing.
Browsing the net, I found on another question here on stackoverflow, the possibility of using: html_entity_decode
But if I echo in frontend, it works perfectly, but when I click on the link it is converted back to: #038;
example:
<?php
$url = '?r='.$prezzo_finale.html_entity_decode('&').'s='.$art_p;
echo $url;
?>
return me in frontend with echo: ?r=405&s=Sito Web
but when i click on my post the permalink is: /?r=405#038;s=Sito%20Web
To pass the variable inside my post I use this structure:
<div class="card" id="professionista<?= $query->post->ID ?>" style="cursor: pointer;">
<!-- CODE -->
<script type="text/javascript">
document.getElementById("professionista<?= $query->post->ID ?>").setAttribute('onclick', 'location.href = "<?php echo esc_url( get_permalink($query->post->ID).$url ); ?>"');
</script>
</div>
How can I get this converted?
I need to pass a variable inside the permalink that takes me to my custom post, and then execute the $_GET
of this variable inside an input of my cf7.
The only problem Wordpress encodes the &
in #038;
I tried to decode the special characters but nothing.
Browsing the net, I found on another question here on stackoverflow, the possibility of using: html_entity_decode
But if I echo in frontend, it works perfectly, but when I click on the link it is converted back to: #038;
example:
<?php
$url = '?r='.$prezzo_finale.html_entity_decode('&').'s='.$art_p;
echo $url;
?>
return me in frontend with echo: ?r=405&s=Sito Web
but when i click on my post the permalink is: /?r=405#038;s=Sito%20Web
To pass the variable inside my post I use this structure:
<div class="card" id="professionista<?= $query->post->ID ?>" style="cursor: pointer;">
<!-- CODE -->
<script type="text/javascript">
document.getElementById("professionista<?= $query->post->ID ?>").setAttribute('onclick', 'location.href = "<?php echo esc_url( get_permalink($query->post->ID).$url ); ?>"');
</script>
</div>
How can I get this converted?
Share Improve this question asked Jan 25, 2021 at 10:59 user14985037user14985037 311 bronze badge 7 | Show 2 more comments1 Answer
Reset to default 1You're setting the link using PHP-generated JavaScript. I'm sure there are better ways to do this, e.g. just write the onclick handler into the div, or use an <a> tag for a link as usual, but if you want to it this way you should be using esc_js() not esc_url().
<script type="text/javascript">
document.getElementById("professionista<?= $query->post->ID ?>").setAttribute(
'onclick', 'location.href = "<?php echo esc_js( get_permalink($query->post->ID).$url ); ?>"');
</script>
As also discussed in the comments you should probably also be using add_query_arg() to append the &s= part to your URL, rather than just string concatenation. That would also handle the urlescape-ing the value part of $url if you're not doing that already, and would cope with the case where your permalink doesn't contain a ? already which it looks like you're assuming.
add_query_arg
to append a URL parameter to a URL? I notice too that the place that nowhere in your question is there code that adds this ampersand in its final context, so we don't know how, why, or where you are attempting to add the ampersand – Tom J Nowell ♦ Commented Jan 25, 2021 at 11:09&
then? Your question does not contain the problematic code, only a single line generic example. Nowhere is it made clear where$url
comes from or how it's created, if your example is what you actually have in your code then the solution is simple, just include&
directly – Tom J Nowell ♦ Commented Jan 25, 2021 at 11:21