David Artiss

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 😉

Categories: WordPress

What are Adobe up to? » « Which Android?

1 Comment

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

Talk to me!

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

Copyright © 2018 David Artiss

Theme by Anders NorenUp ↑

Scroll Up
%d bloggers like this: