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.
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:
- Log into your WordPress admin area
- Click the Appearance option on the left
- Click the Editor option under Appearance
- Under the Templates section on the right, click the "Theme Functions" link
- 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('“', '"', $content);
    $content = str_replace('”', '"', $content);
    $content = str_replace('‘', ''', $content);
    $content = str_replace('’', ''', $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
- Log into your WordPress admin area
- Click the Plugins option on the left
- Near the top of the page, click Add New
- In the search field, type "Curly Quote"
- Locate the plugin you want to use and click Install Now
- 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).
Figure 2. Options for the No Curly Quotes Plugin
Related Resources
- WordPress Codex: add_filter() – describes the add_filter() function provided by WordPress.
- WordPress Codex: Template Tags – provides a list of tags that could potentially be used with add_filter() to turn off curly quotes.
2 Comments
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.
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.
Leave a Comment