Disable wptexturize() in WordPress
I love WordPress. I’ve been using it for years on countless websites and it’s great to see that it still gets better with every new release. Unfortunately, as great as it might be for average users, WordPress can also be quite frustrating for advanced users. If you ever tried to include complex code listings in your posts, then you know what I mean. Under the hood there’s too much functionality that ‘does the thinking for you’ and often results in frustration. On the positive side, at least WordPress offers extensive APIs and resources so you can simply customise it to your needs.
A feature that can be very annoying is the post content processing. When you create a post in the backend it doesn’t necessarily mean that it will look the same in the frontend. Before it appears on your website, WordPress runs your post through a number of filters that replace characters and HTML tags, because WordPress thinks it will look better this way. Most annoying, even after all these years and numerous user complaints, they still don’t offer an easy option to turn it off – requiring you to install a plugin or to edit your template files in order to get rid of it.
Recently I worked on a WordPress template that used Cufón for font replacement and I experienced a problem where the minus character (HTML number 45) was never displayed, even though the font I used contained the sign.
When I looked at the source code of my page, I noticed that WordPress had replaced all special characters with HTML number codes. But what’s more interesting, it turned out WordPress actually replaced all minus characters with dashes (HTML number 8211). While I can understand that this makes sense from a grammar point of view and most word processing software behaves similarly, the problem for me simply was that my font file didn’t include the dash as character, so I wanted to keep the minus ones.
The WordPress function that is responsible for replacing the characters here is wptexturize(). As mentioned before there’s no option to turn it off in the backend, but if you’re happy to edit your template files you can simply add a few lines to the functions.php in your template directory.
To disable the filtering of your post content, add
remove_filter('the_content', 'wptexturize');
To disable it for your post title, add
remove_filter('the_title', 'wptexturize');
To disable it for the post excerpt, add
remove_filter('the_excerpt', 'wptexturize');
Notice that you simply pass in a template function like the_title() or the_content(). You get the idea. This can also be used to disable filtering of comments.