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"
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 &
. 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('&', '&', 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 tourlencode
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:
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: