最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

permalinks - Wordpress | Convert #038; to &

programmeradmin0浏览0评论

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=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('&#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 ); ?>"');
</script>
</div>

How can I get this converted?

Share Improve this question asked Jan 25, 2021 at 10:59 user14985037user14985037 311 bronze badge 7
  • 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 Commented Jan 25, 2021 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 Commented Jan 25, 2021 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 Commented Jan 25, 2021 at 11:21
  • 1 esc_url - . Encodes ampersands (&) and single quotes (‘) as numeric entity references (&#038, &#039). - developer.wordpress/reference/functions/esc_url – Q Studio Commented Jan 25, 2021 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 Commented Jan 25, 2021 at 11:39
 |  Show 2 more comments

1 Answer 1

Reset to default 1

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.

发布评论

评论列表(0)

  1. 暂无评论