Recently, I was writing an article containing programming code on WordPress website. The code contained some single and double quotes and noticed that when I copied the code from the article and pasted it into a code editor and then tried to run it, the code threw syntax errors. In the end, it turned out that the WordPress was automatically changing some characters.
What WordPress does by default is it transforms some of the common plain text characters into formatted entities, among them the quotation marks (both double and single quotes) that are transformed into a nicer looking Smart and Curly Quotes.
To solve this issue, we can either install a plugin for that purpose or add the code manually in the WordPress theme. Each approach work in different way.
Using a plugin
There are quite a few plugins for this task. The most recent ones are:
The Raw HTML plugin gives you ability to either:
- Wrap the programming code with
- Disable wptexturize individual posts in the post editor as shown below:
If you only care for the quotation marks, then the wpuntexturize plugin might be for you. This one only prevents modifications of single and double quotes and it leaves other transformations alone.
Disable the transforms globally
To disable wptexturize feature in WordPress without the plugin and globally across the theme, we can add the following code inside the
add_filter( 'run_wptexturize', '__return_false' );
Disable the transforms only for specific sections
We can also define more precisely where we want to remove the wptexturize. For example, if we only need to disable it in the content, we would do:
remove_filter( 'the_content', 'wptexturize' );
And for the title:
remove_filter( 'the_title', 'wptexturize' );
add_filterfunction with the
Disable the transforms only on specific template file
we can also disable the text transformations in specific template files. For example, for posts only, we would edit the
single.php file and do something like this:
For the content:
remove_filter( 'the_content', 'wptexturize' ); the_content(); add_filter( 'the_content', 'wptexturize' );
And for the title:
remove_filter( 'the_title', 'wptexturize' ); the_title(); add_filter( 'the_title', 'wptexturize' );
In the above code, we turned the wptexturize off for specific section, displayed that section and turned that feature back on.