HTML entities in WordPress titles & Social Media

617 Views Asked by At

First, I'm not an experienced developer. In fact, most of what I do builds on hacks I find on Stackoverflow and things I learn by trial and error — you may see some ugly code ahead.

What I'm trying to do is create a simple way for visitors to share links to social media: Twitter, Facebook, Email.

Here's the code snippet I have now:

        echo ('Share: <a href="http://www.facebook.com/sharer.php?u=');
        echo urlencode(the_permalink());
        echo ('&t=');
        echo themeprefix_social_title( get_the_title() );
        echo ('">Facebook</a> &#183; ');
        echo ('<a href="http://twitter.com/share?text=');
        echo the_title();
        echo ('&url=');
        echo urlencode(the_permalink());
        echo ('&via=REDCAT&related=');
        echo urlencode("REDACT");
        echo ('" title="Share on Twitter" rel="nofollow" target="_blank">Twitter</a> &#183; ');
        echo do_shortcode('[cpss]');

themeprefix_social_title is a function I found here on Stackoverflow for dealing with ampersands in titles:

function themeprefix_social_title( $title ) { $title = html_entity_decode( $title ); $title = urlencode( $title ); return $title; }

It dealt well with ampersands, but it completely messes with quotes and double quotes so they are rendered like the following one (for Twitter):

Why It&#82 17;s Hard (space between "2" and "1" because otherwise it's rendered correctly here)

So now I find myself stuck: If I don't use the function, titles with an ampersand get cut at the point it appears, so for a title like:

The Big & Small & Red

I'd only get:

The Big

However, if I do use the function, my quotes and double quotes get messed up.

0

There are 0 best solutions below