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.
yup.. I was having the same problem..
thank you so much.
Thank you very much! Just a solution I had been looking for!
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’
Thanks for the tip. Added.
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!