Replace Smart/Curly Quotes in WordPress With Regular Ones
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.
February 26, 2024
Thank you for this post. But I'm interested in the opposite function: replacing straight quotes with curved apostrophes.
May I just inverse the two characters in the code ?
February 26, 2024
You're very welcome!
To answer your question, reversing the symbols wouldn't work quite as you'd hope. Each of the quotes are differeny symbols; opening and closing quotes. So you'd need some extra logic to account for that and look for quotes.
That's tricky, though, because lots of things use quotes. Images, for example, and hyperlinks. So, if your code accidentally replaces those with smart quotes, your links and images stop working.
I think it's best to keep it consistent (not mixing cases of smart quotes and regular quotes). For simplicity's sake, I'd recommend sticking with regular quotes and replacing smart quotes. Though perhaps somebody else will chime in and have a different opinion.
October 30, 2024
Is there any way you could remove those smart quotes from the backend?
November 29, 2024
Hey Dahlia!
You can take out the smart quotes from your WordPress backend easily. I usually use a plugin or add a line to the functions.php file; it changes them for you automatically. This way works really well for many of my clients.
Are you working on any specific WordPress project right now? Let me know if you need any more help! 😊