Prevent WordPress From Converting Double Hyphen (‐‐) Into Dash (–)

Prevent WordPress From Converting Double Hyphen Into Dash

This short post covers how to stop WordPress from converting double hyphens (‐‐) to single long dash (–)

Usually I see -- referred to as double dash, and I’m not sure which if I’m correct in calling them hyphens. Nevertheless, I hope this post helps you and please correct me in the comments, if you know the proper way of calling them.

I’ve encountered this issue most often when trying to render text in syntax highlighters, or when using the WordPress Classic Editor in Text Mode.

The solutions I use are not optimal, most likely, but have worked for me and I think and are user friendly, as they don’t require you to create a plugin yourself or edit WordPress core files.

I consider them as a temporary solution to be able to get on with your work instead of spending time finding the ideal fix.

1. Using the Hyphen HTML Code

This is the easiest way.

You just have to use the Hyphen HTML Code in your HTML (I’m using the WordPress Classic Editor, and write directly in Text Mode, and not Visual).

It also renders correctly in Post Titles and Excerpts.

You can simply write ‐‐ or ‐‐ and WordPress will render ‐‐

‐ is more persistent than ‐ (If I try to highlight it with the dark blue background, WordPress renders it as a hyphen)

2. Using Raw HTML Free WordPress Plugin

I use the Raw HTML plugin by By Janis Elsts from the WordPress Plugin Repository, and the Classic WordPress Editor (I’m not sure if there’s a different behavior in Gutenberg), and Enlighter Plugin for syntax highlighting.

[powerkit_alert type=”warning” dismissible=”false” multiline=”true”]This doesn’t work for Post Titles or Excerpts.[/powerkit_alert]

After you install it, you can just wrap any part of your post in the [raw]...[/raw] tags.

In my case, to prevent hyphen conversion into dash, when writing code. The way I prevent with this plugin is as follows:

sudo firewall-cmd [raw]--[/raw]reload

sudo firewall-cmd --reload

..instead of..

sudo firewall-cmd --reload

sudo firewall-cmd – reload

Some of the solutions that I’ve frequently encountered in most Google search results

Not Using – Removing the wptexturize() filter

Why I’m not doing this: I’m not very knowledgeable in WordPress, but from the WordPress Code Reference on wptexturize where it says Replaces common plain text characters with formatted entities, if you scroll a bit on that page you can see that disabling the filter will prevent WordPress from formatting other plain text characters that I may still want to be formatted.

wptexturize() list of formatted HTML entities

So I can’t see how this solution would be a proper fix for this matter.

Not Using – Editing the wp-includes/formatting.php file

Why I’m not doing this: Again, I’m not a WordPress developer, but if I edit that file and it works, and then I update WordPress, I suspect that file may be updated and all my changes lost.

In the Updating WordPress page on WordPress.org they start off with a warning:

[powerkit_alert type=”warning” dismissible=”false” multiline=”true”]WARNING: The upgrade process will affect all files and folders included in the main WordPress installation. This includes all the core files used to run WordPress. If you have made any modifications to those files, your changes will be lost.[/powerkit_alert]

Additional Notes & Updates

In some cases I’ve seen that the double hyphen is kept intact without any of the listed workarounds, however I haven’t been able to pinpoint the exact scenarios when this happens. Perhaps someone can help? Any solutions are more than welcome.

I’ll try to keep this article updated.

Conclusion

Again, I’m not saying these are the best solutions/workarounds, but they’ve worked for me since I first encountered the hyphen conversion issue.

I’m sure there must be others suggesting better fixes that I may have overlooked.

If you have feedback, criticism, or a better solution then I’d love to hear from you.

Feel free to let me know in the comments or by contact us.

0 Shares:
Subscribe
Notify of
guest
Receive notifications when your comment receives a reply. (Optional)
Your username will link to your website. (Optional)

0 Comments
Inline Feedbacks
View all comments
You May Also Like