Replace Smart/Curly Quotes in WordPress With Regular Ones

James Parsons by James Parsons Updated Feb 19th, 2024

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

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 Entries

Written by James Parsons

James Parsons is the founder and CEO of Content Powered, a premier content marketing agency that leverages nearly two decades of his experience in content marketing to drive business growth. Renowned for founding and scaling multi-million dollar eCommerce businesses through strategic content marketing, James has become a trusted voice in the industry, sharing his insights in Search Engine Watch, Search Engine Journal, Forbes, Entrepreneur, Inc, and other leading publications. His background encompasses key roles across various agencies, contributing to the content strategies of major brands like eBay and Expedia. James's expertise spans SEO, conversion rate optimization, and effective content strategies, making him a pivotal figure in the industry.