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());?>&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.

Read More

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.

Related posts

Leave a Reply

1 comment

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