Wordpress | Convert #038; to &

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('#038;').'s='.$art_p;
echo $url;
?

return me in frontend with echo: ?r=405s=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?

Topic formatting permalinks Wordpress

Category Web


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

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.