Replace Smart/Curly Quotes in WordPress With Regular Ones

Posted 02/19/2024, Updated 02/19/2024 James Parsons by James Parsons 2

WordPress, being a WYSIWYG (What You See Is What You Get) text editor, has a tendency to paste unwanted characters along with your content when pasting content into WordPress.

One of the most famous examples of these are "smart" quotes, also known as curly quotes, like these:

“ ”

These should be:

" "

Can you spot the difference? Most people can't. But these two sets of symbols are technically different, and it can be annoying when they don't match. Grammarly even flags these as an error, "Inconsistent punctuation".

"You used two different styles of apostrophes or quotation marks in your document. Both styles are acceptable, but it's best to be consistent."

Inconsistent Punctuation

How do we fix this? Well, check this out. This code, once plopped into your theme's functions.php file, makes sure that this never happens again:

/*
 * Replace smart quotes with regular quotes
 */ 

function replace_smart_quotes($content) {
  $content = str_replace("’", "'", $content);
  $content = str_replace(["“", "”"], '"', $content);
  return $content;
}
add_filter('the_content', 'replace_smart_quotes', 1);

It manually replaces all instances of these smart/curly quotes (as well as smart/curly apostraphes) with regular quotes.

Note: this only happens on the frontend, which, when you think about it, is really the part that matters, since it's the part that people and search engines are seeing. You will still see curly quotes on the backend in the WordPress dashboard, but they are silently swapped before the public sees your content.

Is it possible to permanently remove these on the backend too?

Yes, but the reward is not worth the risk, in my opinion. What if it breaks some code that you had in one of your posts? What if you didn't want it to swap those quotes in certain posts, and you need to undo this change? You're out of luck. Replacing it that way is permanent, and you'd have to restore from a backup - not good.

My code, on the other hand, is safe and totally reversible as it isn't altering anything in your database. So, if you decide to remove it, you can do so easily.

Cool, right? One less thing to think about!

Speaking of Grammarly, look at my related code snippet that helps you automatically remove the code bloat that Grammarly likes to sneak into your content. You can check it out here. Nice!

Please let me know what you think and if this helped you! I'd love to hear from you in the comments.

Related Code Snippets

Written by James Parsons

Hi, I'm James Parsons! I founded Content Powered, a content marketing agency where I partner with businesses to help them grow through strategic content. With nearly twenty years of SEO and content marketing experience, I've had the joy of helping companies connect with their audiences in meaningful ways. I started my journey by building and growing several successful eCommerce companies solely through content marketing, and I love to share what I've learned along the way. You'll find my thoughts and insights in publications like Search Engine Watch, Search Engine Journal, Forbes, Entrepreneur, and Inc, among others. I've been fortunate to work with wonderful clients ranging from growing businesses to Fortune 500 companies like eBay and Expedia, and helping them shape their content strategies. My focus is on creating optimized content that resonates and converts. I'd love to connect – the best way to contact me is by scheduling a call or by email.