I was trying to use WordPress post titles as email subject headers in such a way:
$subject = get_the_title(); wp_mail( $to, $subject, $message);
However this became a problem when the post title included smart quotes, em-dash, ellipsis and the likes of them. My email subject ended up looking like this:
Hey – This is my Post’s Title
where it should have been:
Hey - This is my Post's Title
Well, I solved this by using the php function html_entity_decode
before sending the $subject
to wp_mail()
:
$subject = html_entity_decode( get_the_title(), ENT_QUOTES, 'UTF-8' )
ENT_QUOTES
denotes converting both double and single quotes. The last parameter defines the character set – it is set as UTF-8 since smart quotes and em-dashes are part of the UTF-8 set.
March 17, 2018 at 6:40 pm
yup.. I was having the same problem..
thank you so much.
November 25, 2013 at 12:38 am
Thank you very much! Just a solution I had been looking for!
August 12, 2013 at 7:38 pm
Hi and thanks for the tip. Found it while searching for a solution to my problem. Implemented your code and that fixed the problem.
Tip: Add some tags so more people can find you post like ‘wp_mail’, ‘garbled text’, ‘unicode’
August 29, 2013 at 3:30 pm
Thanks for the tip. Added.
October 10, 2012 at 3:52 am
This is especially helpful if you are doing WPDB queries. For some reason, even though the output was correct, the query would result in nothing unless the ‘get_the_title()’ was properly decoded before passing through the query.
Great tip!