Issue with including encoded text string into twitter sharing button

So I am attempting to use the twitter tweet button to tweet posts from a webpage. I am using the stock standard code straight from Twitter.

Assuming…

Read More

post_text = We’ve only got a “few characters” to explain things here

<a class="twittersocial" onClick="_gaq.push(['_trackEvent', 'Twitter', 'Share', '<?php echo get_permalink($post->ID)?>']); window.open('https://twitter.com/share?url=<?php echo urlencode(get_permalink($post->ID))?>&text=<?php echo urlencode(substr(get_field('post_text'),0,86))?>...&via=twitterhandle&hashtags=newpost','Ratting','width=550,height=400,0,status=0,scrollbars=1');"href="javascript:void(0);" name="Share a quote on Twitter" title="Share a quote on Twitter" ></a>

Result in twitters pop up window:

We've only got a &quot;few characters&quot; to explain things here… http://www.domain.com/newpost-x/ #newpost via @twitterhandle

What would be the best solution to ensuring the post isn’t contaminated with the HTML character markup as seen here? I’m looking to make the solution as light as possible also as I’m replacing what was a very heavy social media plugin.

Related posts

Leave a Reply

2 comments

  1. SOLUTION

    After checking the encoded URL string it seems that ' from get_field was being represented as ' and subsequently encoded as %26%23039%3B (Encoded twice)

    html_entity_decode was used to decode ' to ' prior to encoding it back to '

    substr(html_entity_decode(get_field('post_text'), ENT_QUOTES),0,156)