How to Turn Off Curly Quotes in WordPress so They Don’t Break Your PHP Tutorials

Have you ever copied PHP code from a website tutorial, but no matter what you did you couldn't get it to work? Or maybe you're posting code on a WordPress blog and can't figure out why people are saying the code doesn't work? Well you're not alone. You may have been bitten by the curly quote bug in WordPress.

Background

OK, it's not technically a bug, but its irritating none-the-less. If you haven't posted to WordPress before, it automatically changes all your single and double quotes to curly (or smart) quotes (see Figure 1). The curly quotes may look nice in regular text posts, but they prevent PHP code from working properly.

Screenshots showing curly versus straight quotes
Figure 1. Curly vs. Straight Quotes

For some reason, WordPress doesn't provide any settings to turn off curly quotes like they do for the emoticons. However, there are ways around this.

Note to WordPress.com Users

The following options don't seem to be available for the FREE version of WordPress.com. I'm not sure about the paid version.

Option 1: Custom Function

If you're feeling bold, you could add a little code snippet (developed by Lonewolf Designs) to your functions.php page which replaces curly quotes with straight ones. First we need to define a function which does the replacement:

function removesmartquotes($content) {
     $content = str_replace('“', '"', $content);
     $content = str_replace('”', '"', $content);
     $content = str_replace('‘', ''', $content);
     $content = str_replace('’', ''', $content);
    
     return $content;
}

Then you just need to declare the function with WordPress using its add_filter() function:

add_filter('the_content', 'removesmartquotes');

You can remove the curly quotes from other aspects of your blog by replacing the first argument of the add_filter() function with a different WordPress Template Tag. For example, to remove curly quotes from the comments section you would add the following code:

add_filter('comment_text', 'removesmartquotes');

Once you're ready to add the code to your function.php page, the file can be found using FTP or by following these steps:

  1. Log into your WordPress admin area
  2. Click the Appearance option on the left
  3. Click the Editor option under Appearance
  4. Under the Templates section on the right, click the "Theme Functions" link
  5. Add the code and click the "Update File" button

Note: the code needs to be added between the open and close tags for PHP (<?php ?>). Otherwise the code snippet will break the layout of your WordPress blog.

<?php
function removesmartquotes($content) {
     $content = str_replace('&#8220;', '&quot;', $content);
     $content = str_replace('&#8221;', '&quot;', $content);
     $content = str_replace('&#8216;', '&#39;', $content);
     $content = str_replace('&#8217;', '&#39;', $content);
     
     return $content;
}
 
add_filter('the_content', 'removesmartquotes');
add_filter('comment_text', 'removesmartquotes');
?>

Option 2: Plugins

If you're looking for a more straightforward solution, there are several WordPress plugins available to turn off curly quotes. To install a plugin

  1. Log into your WordPress admin area
  2. Click the Plugins option on the left
  3. Near the top of the page, click Add New
  4. In the search field, type "Curly Quote"
  5. Locate the plugin you want to use and click Install Now
  6. Verify that it's ok to install the plugin and activate it

For more control over which parts of the post don't have curly quotes, you might want to check out the "No Curly Quotes" plugin. Once activated, you can click the "No Curly Quotes" option under Settings. Then indicate whether to show curly quotes in post titles, post content, excerpt, or comments (see Figure 2).

WordPress screenshot showing the options for the No Curly Quotes plugin
Figure 2. Options for the No Curly Quotes Plugin

Related Resources

2 Comments

  • #1 umesh on 03.08.14 at 2:52 am

    hi..
    my blog is all about programming related to blogger stuff, after migrating blogger to wordpress, i am start facing issue with the code quotes, every visitors commenting that this tricks is not working. but after getting this solution from this post, i am really a very happy. thanks for this tutorial, god bless u.

  • #2 Rich Newman on 04.19.18 at 10:40 am

    For free WordPress you can just do a search and manually replace the quotes in the HTML view of your blog post: replace " with " as described above. Or copy the HTML into a text editor, do a global search and replace and then copy it back.

Leave a Comment