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;


$url = '?r='.$prezzo_finale.html_entity_decode('&#038;').'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 ); ?>"');

How can I get this converted?

  • 1
    I think there's some confusion here going on, but it's all completely unnecessary. Why not just use 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 Jan 25 at 11:09
  • hmm I've just noticed you mentioned CF7, you need to contact CF7 support, this isn't a WordPress issue, it's a CF7 issue – Tom J Nowell Jan 25 at 11:09
  • 1
    If that's so can you share how you're adding the & 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 Jan 25 at 11:21
  • 1
    esc_url - . Encodes ampersands (&) and single quotes (‘) as numeric entity references (&#038, &#039). - developer.wordpress.org/reference/functions/esc_url – Q Studio Jan 25 at 11:34
  • 1
    It looks like you're using esc_url somewhere where you should be using urlencode. Or double-encoding a value. – Rup Jan 25 at 11:39

You'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 ); ?>"');

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.

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.