Twitter I hate this fecking cold. 5 days later and it won't go. #fb 1 week ago


Oct 09
28th

Decoding a WordPress Post Title


By default, get_the_title and the_title will return the title of the current post. It’s stored in the database in plain text, however, when returned using the aforementioned functions if appears to be encoded. This means that characters such as ampersands and apostrophes will be converted to equivalents that are more HTML friendly.

Unfortunately, passing, say, this title to Twitter, via the URL, causes problems. First you have to URL encode and the mixture of the HTML encoding and the URL encoding produces a mess that Twitter simply doesn’t cope with very well.

However, the standard HTML decoding in PHP didn’t seem to work 100% with, for example, apostrophes not being decoded. After much head scratching and some frustration I found that this was due to the parameters that I was using.

The following line will correctly decode the post title to plain text…

html_entity_decode(get_the_title($post->ID),ENT_QUOTES,'UTF-8');

To then encode is to be passed via URL, then simply use this…

urlencode(html_entity_decode(get_the_title($post->ID),ENT_QUOTES,'UTF-8'));

It seems obvious now ;)


Related posts:
  1. Simple Twitter Link – WordPress Plugin “Simple Twitter Link” allows you to add a link to your blog that will send the URL of the page/post to your Twitter status....
  2. WordPress Updates You’d think I enjoyed dabbling with my WordPress theme and plugins wouldn’t you Ok, yes, of course I do. It’s great fun. The smiley above...
  3. Plugin error “cannot yet handle MBCS in html_entity_decode” Today I’ve had reported to me that one of my plugins (Simple Twitter Link – but I’m sure this equally occur in others) is generating...

Share with Delicious Share with Digg Share with Facebook Share with LinkedIn Share with MySpace Share with reddit Share with StumbleUpon Share with Twitter

Comments

There are no comments at present.

Pingbacks & Trackbacks

There are no pingbacks or trackbacks at present.

Leave a Comment

All these comments are validated before being placed on the site. I will NOT allow any spam to be shown, I do not follow the links myself and obvious self-promotion of your site is not accepted either.

CAPTCHA Image CAPTCHA Audio
Refresh Image

56 queries in 2.263 seconds.