back home

Ain’t Apostrophes (and Failure) Annoying?

Written by Bunkers on March 18, 2017

For the third time since starting this project I've failed to start 'work' before 5:30am. Each time I've failed to do this I've only just missed it - getting going by 5:45am or 6:00am. It's occasionally crossed my mind to cheat, but thankfully I've realised the cliché of "only cheating yourself" is true.

Despite these failures, I feel I'm starting to cultivate a habit. The main reasons for not getting up are all related to the night's sleep I had. I've been up with one of my daughters, or working hard on something and I've gone to bed late.

The goal is to complete a streak of 66 days. Why 66? Something to do with the average number of days it take to form a habit, and is the default in HabitBull - the app I'm using to track it.

I've combined this goal/habit with a number of others that now makes up a morning 'ritual'. I wake up, drink some water, get dressed, write a blog post, prepare for the kids' breakfast, etc. I think it's this routine that is causing the habit to stick more quickly. It's not a single action I'm trying to achieve when I wake up and something about the prospect of failing at multiple things makes me get out of bed - even if I am late!

De-texturizing Apostrophes

Why did I go to bed late last night? In a word, apostrophes. I'm using a font available on Google Fonts called Poppins in my current WordPress project. I've downloaded the TrueType version from GitHub and installed it on my system. I need to investigate if this is the problem, but the single right quote is rendering in an odd position, below the previous character.

WordPress does something similar to Microsoft Word's smart quotes feature by doing what it calls 'texturizing' your content before it's displayed. This process searches for a number of different characters like quote marks and apostrophes, and changes them in to the appropriate open and closing versions. You can read more about it on the codex page for wptexturize.

Unfortunately this process doesn't provide any filters apart from a check to see if it should run at all. You can use that to disable the process altogether:

add_filter( 'run_wptexturize', '__return_false' );

But I'm happy with the other substitutions, so I decided to try and reverse the process just for apostrophes. The first decision was which filter/hook should I use. I settled on the_content as it's run across the main content of the page before it's displayed. This isn't going to catch titles, excerpts or comments, but I could achieve this by using the same filter function.

The solution then is a simple regular expression. The HTML entity that ends up in the content is ’, or a single right quote. We're looking to change this to the standard apostrophe character '

I wanted to reverse the substitution only for apostrophes so came up with a regular expression. The process boils down to a simple call to preg_replace

function convert_apostrophes($content){
    return preg_replace('/(\w)’(\w)/i',"$1'$2",$content);

This looks for any right quote characters with a 'word' character either side of it. That is a non-white-space character. It then replaces the right single quote with an apostrophe entity.

Further de-texturizing

The less sleep deprived than me amongst you will notice I made a slightly dumb decision. I think the time of night caused me to fixate on just doing this for apostrophes in words, forgetting that the same problem would occur when I want to put a word or phrase in single quotes. I'll fix this later today by using a similar technique to de-texturize all the single quotes in the content. I'm not sure if it would be better to write my own version of wptexturize just handling double quotes and turn off the default one. The other solution of course is to see if I can fix the font! But it's a good exercise in manipulating the content rendered by WordPress.