为 Twitter 共享 URL 编码 get_the_title() [英] Encoding get_the_title() for Twitter share URL

查看:19
本文介绍了为 Twitter 共享 URL 编码 get_the_title()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Twitter 上使用带有弹出式共享按钮的直接链接:<a href="https://twitter.com/intent/tweet?url=<?php echo urlencode(get_permalink());?>&amp;text=<?php echo get_the_title(); ?>&amp;via=username" target="_blank"

有了所有疯狂的社交媒体插件,这是我实现分享按钮的最简单、最直接的方法.

但是,当标题中有 & 时,标题会破坏应该为状态显示的文本.我知道您需要 urlencode() 标题,但是当我这样做时,它会在状态消息中显示特殊字符.

由于现在 Twitter 共享中的 [space] 不需要 +,我需要替换任何 &'s 与 &amp;.但是,它似乎不适用于 str_replace().由于某些原因,它会输出 HTML 特殊字符.做一些类似 <?php echo str_replace('&', '&amp;', urldecode(get_the_title()));?> 也不起作用.

解决方案

& 在 URL 中具有特殊含义,用于分隔多个参数.如果您的 URL 是 ...a&b...,则不代表文本a&b",它代表两个单独的参数.为避免这种情况,您需要对文本进行 urlencode,从而将 & 转换为 %26.

& 编码为 &amp; 仅对于 HTML 中的 & 字符是必需的,您不应在文本中使用这些字符,因为所有 & 都被 %26 替换.

我不知道 Wordpress 的复杂性,但我认为 get_the_title() 返回一个已经被 HTML 转义的值.意思是,它包含 &amp; 而不是 &,所以如果你像这样发送它,这就是发送到 Twitter 的值.您想要的是获得没有 HTML 实体的文本,即您需要对其进行 HTML 解码以将 &amp; 转回实际值 &.然后需要对 HTML 解码的值进行 URL 编码,这样就不会破坏您的 URL 语法.

综合起来,这应该是可行的:

echo urlencode(html_entity_decode(get_the_title(), ENT_COMPAT, 'UTF-8'));

可能还有其他方法可以直接从 Wordpress 获取没有 HTML 实体的原始标题,而无需执行复杂的 html_entity_decode 舞蹈,但同样,我对 Wordpress 了解不多.<小时>

PS:由于您将该 URL 输出到 HTML 上下文中,您应该再次对 URL 进行 HTML 编码,以确保它不会破坏您的 HTML 语法.不过,这主要是为了学术完整性,urlencode() 不应该返回任何可能破坏 HTML 语法的东西.但只是为了迂腐的完整性,这里:

echo htmlspecialchars(urlencode(html_entity_decode(get_the_title(), ENT_COMPAT, 'UTF-8')), ENT_COMPAT, 'UTF-8');

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());?>&amp;text=<?php echo get_the_title(); ?>&amp;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.

Since you do not need a + for a [space] in the Twitter share now, I need to replace any &'s with &amp;. However, it does not seem to work with str_replace(). It for some reasons outputs the HTML special characters. Doing something like <?php echo str_replace('&', '&amp;', urldecode(get_the_title())); ?> doesn't work either.

解决方案

& has a special meaning in URLs to separate several parameters. If your URL is ...a&b..., that does not represent the text "a&b", it represents two separate parameters. To avoid that, you need to urlencode the text, which turns & into %26.

Encoding & to &amp; is only necessary for & characters in HTML, which you should not have in your text, since all &s are replaced by %26.

I don't know the intricacies of Wordpress, but I think get_the_title() returns a value that is already HTML escaped. Meaning, it contains &amp; instead of &, so that's the value that is being sent to Twitter if you send it just like that. What you want is to get the text without HTML entities, i.e. you need to HTML-decode it to turn &amp; back into the actual value &. The HTML-decoded value then needs to be URL-encoded, so it doesn't screw up your URL syntax.

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.


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. That's mostly for academic completeness though, urlencode() should not return anything that can break HTML syntax. But just for pedantic completeness, here:

echo htmlspecialchars(urlencode(html_entity_decode(get_the_title(), ENT_COMPAT, 'UTF-8')), ENT_COMPAT, 'UTF-8');

这篇关于为 Twitter 共享 URL 编码 get_the_title()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆