Website development and design blog, tutorials and inspiration

Removing Smart Quotes from WordPress Posts

Fix that WordPress annoyance and get straight quotes back

Written By on in PHP 1

Removing Smart Quotes from WordPress Posts

172 words, estimated reading time 1 minutes.

WordPress has a nice feature that converts the standard quotes ( " ) to pretty "curly quotes" or "smart quotes" as they are called. This is all well and good on a textual blog, but when it comes to programming code, it can be a real pain in the proverbials.

This little snippet will remove all smart curly quotes and replace them with standard straight quotes. The code needs to be placed within your theme's functions.php.

  1. add_filter('the_excerpt', 'removesmartquotes');
  2. add_filter('the_content', 'removesmartquotes');
  3.  
  4. function removesmartquotes($content)
  5. {
  6. $content = str_replace(""", "& quot;", $content);
  7. $content = str_replace(""", "& quot;", $content);
  8. $content = str_replace("‘", "& #39;", $content);
  9. $content = str_replace("’", "& #39;", $content);
  10.  
  11. return $content;
  12. }

Ironically, Wordpress is messing around with the ampersand, so you must remove the space between the & and the quot and the #39.

All this is doing is creating a new filter for the_content and the_excerpt that does a simple string replace, from curly to straight.

There are other methods, one of which is to use:

  1. remove_filter('the_content', 'wptexturize');

However, this will remove all formatting, including ampersands and so on. I had great trouble with XML after this one, which is why I wrote my own.

Last updated on: Wednesday 21st June 2017

Did you Like this Post? Why not Like us on Facebook?

 

Further Reading
Comments
  1. L2 Lumpy
    L2 Lumpy

    This is exactly what I was looking for! I couldn't figure out why my html in a code box kept getting scrambled when copied and pasted, but it was the "smart curly quotes" you mention not being recognized, so I added your code and no issues now! Also had a problem converting the opening and closing < > characters, but I added your "other" method and that's all good now too. Thank-you, I was pulling my hair out!

Leave a Reply

Your email address will not be published.