Encoding get_the_title() for Twitter share URL Encoding get_the_title() for Twitter share URL wordpress wordpress

Encoding get_the_title() for Twitter share URL


& 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 & 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 & 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 & 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');