CODE or Commands written in your WordPress Site doesn't work after COPY/PASTE? Straight (single and double) Quotes are automatically converted to Curly Quotes or Smart Quotes? Here is why it happens & How to Fix it:
After my post about Changing WordPress URL, I found that a simple COPY/PASTE of the code I provided doesn't actually work, since WordPress auto corrects Straight Quotes (single and double) to Curly or Smart Quotes. Although it looks good and typographically correct, but it doesn't work when used as Programming CODE or Commands. The problem is easy to Fix though:
There are two available methods to disable smart quotes in wordpress:
- Use a WordPress Plugin to Disable Smart Quotes (recommended)
- Edit WordPress Theme functions.php file to Disable Smart Quotes
Follow any one method (not both) that feels most suitable to you. The Plugin method is recommended, since you may want to change the theme in the future & that may affect the results. However, you may not have access to install plugin if it is not your own site, in that case use the Theme Method.
[Method-1] Use a WordPress Plugin to Disable Smart Quotes:
If you don't want to modify any existing theme file (or perhaps you want to apply it to all the themes, so that when you decide to use a new theme, you won't have to do it again), then you can just create a simple WordPress Plugin to disable straight to curly quote conversion. Do the following:
Step-1: Create a new file called disable-smart-quotes.php and use the following CODE:
<?PHP
/*
Plugin Name: Disable Smart Quotes
Plugin URI: http://www.fayazmiraz.com/disable-auto-curly-quotes-in-wordpress/
Description: WordPress Plugin to Disable auto Smart (Curly) quote conversion
Version: 1.0
Author: Fayaz Ahmed
Author URI: http://www.fayazmiraz.com/
*/
if( version_compare ( $wp_version, '4.0' ) === -1 ) {
// To Disable Smart Quotes for WordPress less than 4.0
foreach( array(
'bloginfo',
'the_content',
'the_excerpt',
'the_title',
'comment_text',
'comment_author',
'link_name',
'link_description',
'link_notes',
'list_cats',
'nav_menu_attr_title',
'nav_menu_description',
'single_post_title',
'single_cat_title',
'single_tag_title',
'single_month_title',
'term_description',
'term_name',
'widget_title',
'wp_title'
) as $sQuote_disable_for )
remove_filter( $sQuote_disable_for, 'wptexturize' );
}
else {
// To Disable Smart Quotes for WordPress 4.0 or higher
add_filter( 'run_wptexturize', '__return_false' );
}
Step-2: Then upload this file to the following directory:
Your_WordPress_Installation_Folder/wp-content/plugins/
Step-3: Then, login to your WordPress Admin Panel from Your_WordPress_URL/wp-admin/
» go to Plugins menu » activate this new plugin named Disable Smart Quotes
At this point, your WordPress Smart Quotes are Disabled and WordPress will no linger automatically convert straight (single or double) quotes into curly (single or double) quotes or smart quotes.
[Method-2] Edit WordPress Theme functions.php file to Disable Smart Quotes:
// Add the following lines in the functions.php file of your current WordPress Theme:
if( version_compare ( $wp_version, '4.0' ) === -1 ) {
// To Disable Smart Quotes for WordPress less than 4.0
foreach( array(
'bloginfo',
'the_content',
'the_excerpt',
'the_title',
'comment_text',
'comment_author',
'link_name',
'link_description',
'link_notes',
'list_cats',
'nav_menu_attr_title',
'nav_menu_description',
'single_post_title',
'single_cat_title',
'single_tag_title',
'single_month_title',
'term_description',
'term_name',
'widget_title',
'wp_title'
) as $sQuote_disable_for )
remove_filter( $sQuote_disable_for, 'wptexturize' );
}
else {
// To Disable Smart Quotes for WordPress 4.0 or higher
add_filter( 'run_wptexturize', '__return_false' );
}
If you can't find the functions.php file, look inside the following directory structure:
Your_WordPress_Installation_Folder/wp-content/themes/Your_Current_Theme_Folder/functions.php
If the file doesn't already exist there, then create the file (name it: functions.php) and use the CODE below:
<?PHP
if( version_compare ( $wp_version, '4.0' ) === -1 ) {
// To Disable Smart Quotes for WordPress less than 4.0
foreach( array(
'bloginfo',
'the_content',
'the_excerpt',
'the_title',
'comment_text',
'comment_author',
'link_name',
'link_description',
'link_notes',
'list_cats',
'nav_menu_attr_title',
'nav_menu_description',
'single_post_title',
'single_cat_title',
'single_tag_title',
'single_month_title',
'term_description',
'term_name',
'widget_title',
'wp_title'
) as $sQuote_disable_for )
remove_filter( $sQuote_disable_for, 'wptexturize' );
}
else {
// To Disable Smart Quotes for WordPress 4.0 or higher
add_filter( 'run_wptexturize', '__return_false' );
}
Remember, there should be no space or new-line (or anything else) before the PHP start tag <?PHP
Also, notice that, I haven't used PHP end tag
?>
in my CODE. That's because it's a recommended PHP practice to not to include the end tag if the CODE is pure PHP.
Note: Once you Disable Smart Quotes this way, there may be places where you will need them for proper typography. Since WordPress will not automatically convert them for you, just make sure that you type them correctly when you actually need curly quotes. Here is a guide to type curly quotes correctly.
Thank you very much for sharing and this tip.
It is very helpful and gets me out of the trap.
Thanks for sharing this one!
I had no clue why those quotes were altered by WordPress... (although I am more of a coder than a WordPress user)
Thank you so much!
Thank you!
I was writing an article for my blog that contained a lot of 'key' / 'value' pairs with single quotes and noticed when I copied them into a code, it wouldn't work.
Your solution did the trick.
very goood thanks for this tutorial
Very helpful! Thank you.
Very good solution indeed
The Article Must Interesting For A Web Developer, Thanks Admin
Thanks a lot!
I'm using your script!
BTW: in the code for the plugin, you need to close the PHP section with ?>
Regards,
... Jesús Prieto ...
I'm glad it helped 🙂
Not really, no. In fact, it's a recommended practice to not to add the ending tag if the CODE is pure PHP.
This is what PHP manual says:
You rock! Perfect solution.
Thanks for all the information you give on the blog.
thank you very much by
laura
Thanks for sharing.
I previously worked with wp-themes which had this option enabled by default. So I didn't see the problem until recently.
Thanks to u, i've found the soln quickly.