Problem
I need WordPress to stop turning underscores into hyphens in its slugs/permalinks.
Situation
Moving an old site over to a clean WordPress installation. Old site uses underscores in its urls. We need to preserve the old urls including underscores at this time (please do not recommend any SEO advice here, thank you).
WordPress cleans up its slugs/permalinks. Underscores are turned into hyphens.
I want to be able to use both underscores AND hyphens in my slugs. I am not looking to replace one with the other completely, which is what most suggestions/solutions I’ve found boil down to.
Examples of a solution that does not meet my criteria:
https://wordpress.stackexchange.com/questions/78334/convert-hyphen-to-underscore-in-permalinks
Example of a solution to allow dots that may be useful:
https://wordpress.stackexchange.com/questions/231448/how-to-add-dot-in-post-slug
What I’ve tried
I have searched for a solution but was unable to find a satisfying one. Supposedly the cleaning up of slugs/permalinks is defined in /wp-includes/formatting.php using the sanitize_title_with_dashes function.
I pasted the code related to sanitizing here:
The only line I can see that targets underscores is:
$title = preg_replace( '/[^%a-z0-9 _-]/', '', $title );
For testing purposes I removed the underscore from that line, replaced formatting.php on the server and tried putting underscores in a slug again. But it didn’t do anything. I also only just figured out based on the “allow dots in permalinks” link mentioned earlier, that that line supposedly ALLOWS underscores in urls.
This also fits the comment content “* Limits the output to alphanumeric characters, underscore (_) and dash (-).”
So… this code is saying that underscores are allowed (in slugs/permalinks?), but they’re still being stripped in my slug/permalink.
Can anyone see something here that I cannot. Or suggest a filter that will allow both underscores and hyphens in the slug/permalink? Right now I’m stuck.
Thank you in advance for any help you’re willing to offer.
Solution found
The Gutenberg editor forces hyphens in the slug of a post or page at this time: https://github.com/WordPress/gutenberg/issues/13091
In my case, I have enabled the Classic Editor plugin: https://wordpress.org/plugins/classic-editor/
I’ll rebuild the website and the old urls with underscores that way. When I’m done I’ll switch back to Gutenberg. The old urls will be changed carefully and gradually in the future and the new ones will use hyphens. So it should work out.
Alternatively, as user @admcfajn pointed out, you can modify the page/post slug from the page/post overview screen using the Quick edit functionality. Underscores added to the slug that way will stick, even with the Gutenberg editor active.
If you’re looking to replace hyphens with underscores permanently, create a filter using the solution presented for this question instead:
https://wordpress.stackexchange.com/questions/78334/convert-hyphen-to-underscore-in-permalinks
Thanks to those who took the time to reply (one user deleted their reply or had it removed). I hope others will find this solution through Google in the future.
Hopefully either Gutenberg will be fixed or the comment in the formatting.php file will be amended to avoid confusion if it is the developers’ intent to only allow users to use hyphens.
2
Answers
I update my original post as well, but adding an accepted answer for visibility.
The Gutenberg editor forces hyphens in the slug of a post or page at this time: https://github.com/WordPress/gutenberg/issues/13091
In my case, I have enabled the Classic Editor plugin: https://wordpress.org/plugins/classic-editor/
I'll rebuild the website and the old urls with underscores that way. When I'm done I'll switch back to Gutenberg. The old urls will be changed carefully and gradually in the future and the new ones will use hyphens. So it should work out.
Alternatively, as user @admcfajn pointed out, you can modify the page/post slug from the page/post overview screen using the Quick edit functionality. Underscores added to the slug that way will stick, even with the Gutenberg editor active.
If you're looking to replace hyphens with underscores permanently, create a filter using the solution presented for this question instead: https://wordpress.stackexchange.com/questions/78334/convert-hyphen-to-underscore-in-permalinks
Thanks to those who took the time to reply (one user deleted their reply or had it removed). I hope others will find this solution through Google in the future.
Hopefully either Gutenberg will be fixed or the comment in the formatting.php file will be amended to avoid confusion if it is the developers' intent to only allow users to use hyphens.
We shouldn’t have any problem using both hyphens and dashes with WordPress
The url-slug of
test_page-of-awesome
is perfectly valid. So, I imagine there must be a setting somewhere in a plugin or theme which is rewriting or modifying those.You mention that:
… This is not expected behavior of WordPress and is being caused by a plugin, theme, or something else… But it is not how WordPress is expected to work.
Try the following:
wp-admin/wp-admin/options-permalink.php
Settings >> Permalinks& if you can’t get url-slugs to save then the issue is likely coming from elsewhere on your server. Depending on the configuration of your web-hosting environment there may be something else affecting the url slugs. It might be apache mod_rewrite rules or something