Wednesday Quickie – Dash vs Em Dash
Wednesday Quickie – Modifying WordPress Translation Filters
When I publish a ‘quickie’ I usually try and tie it into the main blog that was published that week. This month’s quickie however will tie into the blog as a whole. This may be small potatoes to 97.3% of my readers, however to me this is ‘YUGE’! It has bothered me for as long as I have used WordPress that double dashes — are translated in to a – em dash. While this may be typographically correct, try it on your Wordprocessor, dollars to doughnuts it will convert it to an em dash as well. There are times when it is just plain wrong, take for instance my *nix tutorials. $somecommand --somefunction is not the same as $somecommand —somefunction. I always felt bad that if you followed my yellow text it would not work (unless you noticed the difference in the screenshot), cause utter confusion, and might even make you think I don’t know what I am doing and not return to the blog. So, I finally decided to DO SOMETHING ABOUT IT! Sitting down in front of Google I was bound and determined to figure this out, what makes WP do this, and most importantly how do I stop it?! This led me to the /wp-includes/formatting.php file.
Taken from the codex on
www.wordpress.com ,https://codex.wordpress.org/Function_Reference/wptexturize. Formatting.php does the following, “This returns given text with transformations of quotes to smart quotes, apostrophes, dashes, ellipses, the trademark symbol, and the multiplication symbol.”
So the lightbult went on that maybe, just maybe I could find the function that performed the conversion. Which I was able to do.
Here is the block we are looking for.
// Dashes and spaces
$dynamic[ ‘/—/’ ] = $em_dash;
$dynamic[ ‘/(?<=^|’ . $spaces . ‘)--(?=$|’ . $spaces . ‘)/’ ] = $em_dash;
# $dynamic[ ‘/(?<!xn)--/’ ] = $en_dash; (Pounded out – 23OCT2018 to prevent WP from translating double dashes into em_dash)
$dynamic[ ‘/(?<=^|’ . $spaces . ‘)-(?=$|’ . $spaces . ‘)/’ ] = $en_dash;
$dynamic_characters[‘dash’] = array_keys( $dynamic );
$dynamic_replacements[‘dash’] = array_values( $dynamic );
You can see from the above where the double hyphens are translated into the em_dash. I simply commented the line out, and also added the commend above it (That I will move later!) that can be used if I ever need to go back into the file and remove or change what I added, as I will most likely forget.
I then restarted httpd $sudo systemctl restart httpd command to reload php, and tested and as you can see below. IT WORKED!
So, what if I actually want to use an em dash in one of my posts?
I can still do this by using the actual key combinations.
On OS macOS use Shift-Option – (minus)
On Windows use Alt-Ctl – (minus)
The only issue I have with this modification is that the formatting.php file will most likely be re-written in a major WP upgrade, so every time I upgrade to a newer version I will need to put my mod back in.
Thank you for reading this Brent’s World Wednesday Quickie! We hope you will return for more great content. If you wish to be notified when new content is published, you may register by clicking HERE.
NOTE – In later versions of WordPress, there is an additional line that needs to be #’d out.
# $dynamic[ ‘/(?<=^|' . $spaces . ')--(?=$|' . $spaces . ')/' ] = $em_dash; (Pounded out to stop -- from being translated$ # $dynamic['/(?