Mastering Smart Quotes

by on February 23, 2011

At some point or another, we've all encountered an issue with the WordPress Smart Quotes feature. Despite the occasional issues, they do look nice for our visitors. Most word processors provide these curly quotes as an automatic feature too. We'll present a few solutions for you to choose from. This includes a couple of filters that you might want to use.

We're talking about changing ' to ', ', or '.

It also involves changing " to , , or .

One simple solution is to not use actual single or double quotes where you absolutely need them to appear. Instead, you can use HTML character codes. For example, you can use ' instead of a single quote. And, you can use " instead of a double quote. You can view a table of the various HTML character codes here.

Another possible solution is to completely disable the culprit that converts your quotes to their curly cousins. The function is located in wp-includes/formatting.php; it's the wptexturize() function which is used as an output filter.

To do so, put these lines in your theme's functions.php file. Or, choose the ones you want to use and omit the others. The filter names adequately describe where each one applies.

Code: PHP (plus WordPress)

remove_filter('the_content', 'wptexturize');
remove_filter('widget_text', 'wptexturize');
remove_filter('the_excerpt', 'wptexturize');
remove_filter('the_rss_content', 'wptexturize');
remove_filter('the_title', 'wptexturize');
remove_filter('single_post_title', 'wptexturize');
remove_filter('comment_text', 'wptexturize');
remove_filter('comment_author', 'wptexturize');
remove_filter('list_cats', 'wptexturize');
remove_filter('category_description', 'wptexturize');
remove_filter('bloginfo', 'wptexturize');

WordPress maintains a list of all their default filters. You can view it here to get more info on the above filters or to find others you might want to add to the removal sequence.

A Brute Force Filter Option

If that option leaves you lamenting that our beloved blogging engine might not parry a resolute salvo of strategic attacks, you could consider a foray into the realm of constructive alternatives. Our first filter simply offers an UNDO option to the curly quotes … it changes them back into what you actually typed … well sort of.

This is the inverse process of our previous example. Again, omit (or comment out, //) the options that you don't want to implement. Here's the code.

Code: PHP (plus WordPress)Filter: WCS Disable Curly Quotes

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
add_filter('the_content', 'wcs_disable_curly_quotes', 99);
add_filter('widget_text', 'wcs_disable_curly_quotes', 99);
add_filter('the_excerpt', 'wcs_disable_curly_quotes', 99);
add_filter('the_rss_content', 'wcs_disable_curly_quotes', 99);
add_filter('the_title', 'wcs_disable_curly_quotes', 99);
add_filter('single_post_title', 'wcs_disable_curly_quotes', 99);
add_filter('comment_text', 'wcs_disable_curly_quotes', 99);
add_filter('comment_author', 'wcs_disable_curly_quotes', 99);
add_filter('list_cats', 'wcs_disable_curly_quotes', 99);
add_filter('category_description', 'wcs_disable_curly_quotes', 99);
add_filter('bloginfo', 'wcs_disable_curly_quotes', 99);
 
function wcs_disable_curly_quotes($content='')
{
    $content = str_replace(array('“', '”', '″'), '"', $content);
    $content = str_replace(array('‘', '’', '′'), ''', $content);
    return $content;
}
The Selective Filter Option

Both major options thus far are brute force techniques. But, the method we prefer … and you may too … is a selective filter technique. In essence, we'll enclose the content we want unscathed by “The Curly Quotes Initiative” in a special tag sequence. As a bonus, we'll offer additional features.

Here's one example of what we can do with the WCS Curly Quote Enhancement filter. You CAN have curly quotes for most of your content and then turn them off as needed.

This line is not filtered.
“Smart quotes” are bad, ain't they. So … 'this is' a test.

This one is enclosed in [code] … [/code] tags … just like a shortcode.
"Smart quotes" are bad, ain't they. So ... 'this is' a test.

And with the bonus enhancements, copyright [copyright] 2011 becomes copyright © 2011.

And, 6[cubed] = 216 becomes 6³ = 216.

How many times have you struggled to remember the code for copyright (either the HTML character code or the character entity)? How about some of the others like trademark ™ or euro €.

We've included 21 of these auto-conversion-macros as plain English words for you. For clarification, these symbol macros can be inside the new code tags OR anywhere in your content.

If this is the solution you prefer, put it in your theme's functions.php file. You should not combine this filter with either of the two previous major options.

Code: PHP (plus WordPress)Filter: WCS Curly Quote Enhancement

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
add_filter('the_content', 'wcs_curly_quote_enhancement', 99);
add_filter('widget_text', 'wcs_curly_quote_enhancement', 99);
add_filter('the_excerpt', 'wcs_curly_quote_enhancement', 99);
add_filter('the_rss_content', 'wcs_curly_quote_enhancement', 99);
add_filter('the_title', 'wcs_curly_quote_enhancement', 99);
add_filter('single_post_title', 'wcs_curly_quote_enhancement', 99);
add_filter('comment_text', 'wcs_curly_quote_enhancement', 99);
add_filter('comment_author', 'wcs_curly_quote_enhancement', 99);
add_filter('list_cats', 'wcs_curly_quote_enhancement', 99);
add_filter('category_description', 'wcs_curly_quote_enhancement', 99);
add_filter('bloginfo', 'wcs_curly_quote_enhancement', 99);
 
function wcs_curly_quote_enhancement($content='')
{
    // init
    $tag = 'code';
    $filter = '#\[' . $tag . '\](.+?)\[\/' . $tag . '\]#is';
 
    // restore our enclosed content
    $content = preg_replace_callback($filter, 'wcs_uncurly_quote', $content);
    // now, strip the [code] tags themselves
    $content = preg_replace('#\[/?' . $tag . '[^\]]*\]#is', '', $content);
 
    // plus, let's process some more enhancements
    $search = array('[bullet]', '[cent]', '[copyright]', '[cubed]',
        '[degrees]', '[dot]', '[ellipsis]', '[euro]', '[half]',
        '[leftangle]', '[paragraph]', '[plusminus]', '[pound]',
        '[quarter]', '[registered]', '[rightangle]', '[section]',
        '[squared]', '[threequarters]', '[trademark]', '[yen]');
    $replace = array('•', '¢', '©', '³', '°',
        '·', '…', '€', '½', '«',
        '¶', '±', '£', '¼', '®', '»',
        '§', '²', '¾', '™', '¥');
    $content = str_ireplace($search, $replace, $content);
 
    return $content;
    /**********************************************************************
     Copyright © 2011 Gizmo Digital Fusion (http://wpCodeSnippets.info)
     you can redistribute and/or modify this code under the terms of the
     GNU GPL v2: http://www.gnu.org/licenses/gpl-2.0.html
    **********************************************************************/
}
 
function wcs_uncurly_quote($matches)
{
    $output = str_replace(array('“', '”', '″'), '"', $matches[0]);
    $output = str_replace(array('‘', '’', '′'), ''', $output);
    return $output;
}

In line #20 we use a regular expression to restore the wptexturized content back to normal quotes … if it's enclosed in [code] … [/code] tags. We complete the process by using a callback to our wcs_uncurly_quote() function. The PHP function preg_replace_callback() is a nifty way to search and replace/modify content.

On line #22, we strip out the code tags themselves. We wouldn't want the tags displayed on our blog. Lines 20 and 22 perform a lot of work for such a terse amount of PHP code. You could easily use them elsewhere (with minor modification) for a variety of possibilities.

Finally, lines 25-34 we perform the remaining enhancements. This is accomplished by building matching search/replace arrays and then calling the str_ireplace() function. With regard to our symbol macros, wptexturize() already handles the trademark symbol and ellipsis dots. This new filter merely provides an added alternative for these two … while adding 19 others for your convenience.

Now … you can blog with little concern about the “Oppressive Curly Quotes Initiative”.

Share This Article: “Mastering Smart Quotes”

(Also Available: Press CTRL+D to Bookmark this Page)

Comments

Share Your Thoughts  One Response to “Mastering Smart Quotes”
  1. 1

Share Your Thoughts

(Some editor features are restricted unless you're logged in.)

(When replying to a specific comment, your browser may require Shift+Enter instead of just Enter.)


(get a gravatar)


Notify me of followup comments via e-mail. You can also subscribe without commenting.