How to Remove Smart Quotes from WordPress
Posted onThe WordPress editor assumes that most people are going to be typing in regular text as part of an article or a regular post, and for the most part this is true. As a result, it tries to make several optimizations that “prettify” what you type in. For example, hitting the “Enter” key will insert a paragraph instead of a newline. At the same time, when rendering a post in its final version, WordPress replaces regular quotation marks
("")
… with smart quotes:
(“”).
These look nicer and also give a clear indication of whether a quotation is beginning or ending. It’s little tweaks like this that make editing in WordPress a breeze.
However, formatting changes like this can land you in problems if you are extremely finicky about how your text renders in its final form. Smart quotes in particular are a special pain point for programmers for whom formatting is absolutely critical. A typical snippet of code is likely to contain several normal quotation marks to delineate strings. While a regular code editing program will never use smart quotes instead of plain quotes, pasting the same into WordPress will cause them to render incorrectly by default.
This is due to a filter running in WordPress called “wptexturize”. In this article, we take a quick look at how to disable it so that WordPress doesn’t automatically prettify regular quotes, as well as how to deal with the issue of converting smart quotes that have already been pasted into WordPress – probably from another website.
Removing the Default “wptexturize” Filter
Let’s take an example and see how this works. In the screenshot below, I’ve typed in a regular phrase with normal quotation marks in the WordPress editor:
But when the post is published, it renders like this:
You can see that the straight quotes have been replaced by “pretty” quotes. However, we can avoid this by selecting our text and choosing “Preformatted” from the drop-down menu of the WordPress editor. If you do this, the straight quotes will render as is:
However, if you paste text already containing smart quotes from another website, marking it as preformatted will not change them into straight quotes as you can see here:
So let’s solve the first problem. Namely, preventing WordPress from replacing straight quotes with smart quotes. Open up your functions.php file, paste the following code at the bottom right before the closing ?> PHP tag and save it:
remove_filter('the_content', 'wptexturize');
This simple tweak prevents WordPress from making any changes to the quotes you manually enter. So here’s what our example looks like with the addition of the code above:
As expected, WordPress has left our straight quotes in the first line intact. However, it has not actively corrected smart quotes and made them straight. And obviously it hasn’t done the same for the final preformatted line either. If you want to tell WordPress to explicitly change smart quotes into regular straight quotes, you need to paste the following code into your functions.php (again before the closing ?> tag). Some of the code here is borrowed from the “Smart Quote Fixer” WordPress plug-in.
function remove_smart_quotes($content) { $content= str_replace( array("\xe2\x80\x98", "\xe2\x80\x99", "\xe2\x80\x9c", "\xe2\x80\x9d", "\xe2\x80\x93", "\xe2\x80\x94", "\xe2\x80\xa6"), array("'", "'", '"', '"', '-', '--', '...'), $content); $content= str_replace( array(chr(145), chr(146), chr(147), chr(148), chr(150), chr(151), chr(133)), array("'", "'", '"', '"', '-', '--', '...'), $content); return $content; } add_filter( 'the_content', 'remove_smart_quotes');
This function hooks into the content filter and actually replaces smart quotes with their straight quote equivalents. Here’s the screenshot of what our output looks like after this latest addition:
You can see that absolutely all smart quotes have been removed from all three lines regardless of the mode of entry. Keep in mind that this is a bit of a nuclear solution – you will never have smart quotes in your site content ever again! It might not be for everyone, but it can certainly remove a great deal of headache for programmers pasting code into WordPress.