Removing Smart Quotes from WordPress Posts

Last Updated January 14, 2019 by Tim Trott. First Published in 2009.

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.

Removing Smart Quotes from WordPress Posts

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.

php
add_filter('the_excerpt', 'removesmartquotes');
add_filter('the_content', 'removesmartquotes');

function removesmartquotes($content)
{
  $content = str_replace("“", """, $content);  
  $content = str_replace("”", """, $content);  
  $content = str_replace("’", "'", $content);  
  $content = str_replace("‘", "'", $content);  

  return $content;
}

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:

php
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.

Comments

This post has 1 comment(s). Why not join the discussion!

We respect your privacy, and will not make your email public. Hashed email address may be checked against Gravatar service to retrieve avatars. This site uses Akismet to reduce spam. Learn how your comment data is processed.

  1. LL

    On Wednesday 18th of July 2012, L2 Lumpy said

    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!