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 😉

2 responses

  1. 7 Years later and this info is still relevant and useful. Thank you David for sharing and keeping this post live 🙂

  2. THANK YOU for this! It’s been driving me crazy and I’m so thankful that I found a solution here 🙂

Leave a Reply to CarlosCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from David Artiss

Subscribe now to keep reading and get access to the full archive.

Continue reading