[英]Encoding get_the_title() for Twitter share URL
I am using a direct link with a popup for a share button on Twitter: <a href="https://twitter.com/intent/tweet?url=<?php echo urlencode(get_permalink());?>&text=<?php echo get_the_title(); ?>&via=username" target="_blank"
我在推特上使用弹出窗口直接链接分享按钮: <a href="https://twitter.com/intent/tweet?url=<?php echo urlencode(get_permalink());?>&text=<?php echo get_the_title(); ?>&via=username" target="_blank"
With all the crazy social media plugins out there, this is the simplest and most direct way for me to implement a share button. 有了所有疯狂的社交媒体插件,这是我实现共享按钮最简单,最直接的方式。
However, when there is an &
in the title, the title will break the text that is supposed to show for the status. 但是,当标题中有&
时,标题将破坏应该显示状态的文本。 I understand you need to urlencode()
the title, but when I do that, it displays the special characters in the status message. 我知道你需要urlencode()
标题,但是当我这样做时,它会在状态消息中显示特殊字符。
Since you do not need a +
for a [space]
in the Twitter share now, I need to replace any &
's with &
既然你现在不需要在Twitter中共享[space]
+
,我需要用&
替换任何&
. 。 However, it does not seem to work with str_replace()
. 但是,它似乎不适用于str_replace()
。 It for some reasons outputs the HTML special characters. 它由于某些原因输出HTML特殊字符。 Doing something like <?php echo str_replace('&', '&', urldecode(get_the_title())); ?>
做一些像<?php echo str_replace('&', '&', urldecode(get_the_title())); ?>
<?php echo str_replace('&', '&', urldecode(get_the_title())); ?>
doesn't work either. <?php echo str_replace('&', '&', urldecode(get_the_title())); ?>
也不起作用。
&
has a special meaning in URLs to separate several parameters. &
在URL中具有特殊含义以分隔多个参数。 If your URL is ...a&b...
, that does not represent the text "a&b", it represents two separate parameters. 如果您的网址是...a&b...
,它不代表文字“a&b”,则代表两个单独的参数。 To avoid that, you need to urlencode
the text, which turns &
into %26
. 为了避免这种情况,需要urlencode
文本,果然&
到%26
。
Encoding &
to &
编码&
&
is only necessary for &
characters in HTML, which you should not have in your text, since all &
s are replaced by %26
. 只对必要的&
在HTML字符,你不应该在你的文字,因为所有&
S被替换%26
。
I don't know the intricacies of Wordpress, but I think get_the_title()
returns a value that is already HTML escaped. 我不知道Wordpress的复杂性,但我认为get_the_title()
返回一个已经HTML转义的值。 Meaning, it contains &
意思是,它包含&
instead of &
, so that's the value that is being sent to Twitter if you send it just like that. 而不是&
,所以如果你发送它就是发送给Twitter的价值。 What you want is to get the text without HTML entities, ie you need to HTML-decode it to turn &
你想要的是获得没有HTML实体的文本,即你需要对它进行HTML解码才能转向&
back into the actual value &
. 回到实际值&
。 The HTML-decoded value then needs to be URL-encoded, so it doesn't screw up your URL syntax. 然后,HTML解码的值需要进行URL编码,因此不会搞砸您的URL语法。
Putting it all together, this should hopefully do it: 总而言之,这应该是有希望的:
echo urlencode(html_entity_decode(get_the_title(), ENT_COMPAT, 'UTF-8'));
There may be other ways to get the original title without HTML entities directly from Wordpress without needing to do the complicated html_entity_decode
dance, but again, I don't know a lot about Wordpress. 可能有其他方法可以直接从Wordpress获取没有HTML实体的原始标题,而无需进行复杂的html_entity_decode
舞蹈,但同样,我对Wordpress知之甚少。
PS: Since you're outputting that URL into an HTML context, you should HTML encode the URL again to make sure it doesn't break your HTML syntax. PS:由于您要将该URL输出到HTML上下文中,因此您应该再次对URL进行HTML编码,以确保它不会破坏您的HTML语法。 That's mostly for academic completeness though, urlencode()
should not return anything that can break HTML syntax. 这主要是为了学术完整性, urlencode()
不应该返回任何可能破坏HTML语法的东西。 But just for pedantic completeness, here: 但只是为了迂腐的完整性,这里:
echo htmlspecialchars(urlencode(html_entity_decode(get_the_title(), ENT_COMPAT, 'UTF-8')), ENT_COMPAT, 'UTF-8');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.