Wordpress: allow underscore in slug - wordpress

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:
https://pastebin.com/jRqV8iB8
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.

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:
Wordpress cleans up its slugs/permalinks. Underscores are turned into hyphens.
... 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:
enable a core-theme ( twentyninteteen, twentytwenty )
disabling all plugins
ensure that you're able to use pretty-permalinks 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

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.

Related

WordPress Zero Width Spaces in URL

We have a user that has zero width spaces that are ending up at the end of URL's that he is copying and pasting from an outlook email client into his WordPress 4.9.4 site. This doesn't happen on all of the sites, just some of them. The issue is happening just on a standard page using the TinyMCE tools to create the link.
Is anyone aware of a way to easily have WordPress strip these out? I don't know what the "correct" behavior is on these - but it certainly causes for some unique troubleshooting cases for the uneducated.
Another way to state this: Is anyone aware what the "default" behavior is on WordPress TinyMCE with handling "zero width spaces"? I have a user running into issues with them (not sure how they are getting there) and I've never seen this before.

Issues with translating certain strings

I'm having a bit of a major meltdown regarding a translation of a site. It's a wordpress site with a WooCommerce shop. LocoTranslate and Avada is installed.
The issue is, that some individual strings of text are not being translated - even when i've changed the .po file (with PoEdit).
For example, there's a string on the customer cart that says:
"hello USER (not USER? Sign out)"
In PoEdit it looks as such:
Hello %1$s (not %1$s? Sign out)
I'm adding a translation that says:
Hej %1$s (ikke %1$s? Log ud)
It's just not being updated on the site!
Does anyone know what I'm doing wrong??
There are 2 other words that won't be translated either :(
I work a lot with dual language WP sites and find this quite often. In most cases it dues to the textdomain being called before the strings in question are being called. In theory this shouldn't happen but it does. The solution that usually works for me is to add the textdomain a second time in yours functions file and use a hook that runs much later in the site loading process. This invariably fixes it although it a bit of a hack.
If this isn't working and you only need the site in one language you can create a child theme and manually translate the stubborn strings in the page template.

'Bug' in WordPress? All URL's are appended with a '-2'

This is odd...
When I create a page (either draft or published) WordPress does its' job by creating/ suggestion a URL File Name.
For some reason it always appends a '-2' (without apostrophes)
So, for example - /exampleABC/ become automatically /exampleABC-2/
At first I thought it was because there was a draft version of the file or one in existence, but that isn't the case.
I've no idea how best to error-check here - any ideas what I should be looking for? Thanks
The current version of WordPress (version 4.5.1) does not have this "bug". Therefore, one of a few possibilities is happening for you:
Possibility 1: You have slug conflicts.
This is the way that WordPress resolves "slug" conflicts. So - the thing that you have to remember is that ANY record in the posts table - whether for a page, post, custom post type, etc - and even media - will have a slug. As #McNab points out - this includes the posts (and media) in the trash. If the slug for the page you are creating conflicts with any existing slug, then WP will automatically append a number (and "increment" it as appropriate):
Saving example - if a post with the slug example exists, will result in a new slug of example-2. If example-2 already exists, then example-3, etc.
Possibility 2: You have a plugin that is causing this issue.
Always keep in mind that plugins can do a LOT, given the power and flexibility of WP, and it's conceivable that a plugin (or your theme) could be causing this.
Possibility 3: Your site is hacked.
Similar to #2, there may be a nefarious / malicious plugin, theme, or "hack" on your site, which is causing this.
However - my strong guess is that it's just possibility 1 - you have other records in the posts table with the slug already existing.
How to know / prove it?
If I were testing / debugging this, I would take the following steps:
Create a page with a crazy, unique page title: "xyzpdqaaa" or similar. Save it. What is the slug? If it's "xyzpdqaaa-2" (aka "wrong"), then possibility one is (probably) ruled out. If it's "xyzpdqaa" (aka "right"), then everything is fine, and you just have slug conflicts.
If the slug turns out to be "xyzpdqaaa-2", then I would disable ALL plugins. (This is a pain, and I hate doing it, but it truly is the way to prove it). Repeat step 1 (with a new "crazy unique" title) - if it's correct, then you've got a plugin causing the problem.
If it is a plugin, start enabling plugins. Typically I work in "halves" - enable HALF the plugins (do your best guess at which one(s) are safe - and enable those first). Test again. If OK, enable more (half of the remaining plugins), test again. If not ok, disable half the enabled ones. (Keep track of which you did / did not enable, so you can narrow down which plugin is causing the issue). Repeat until you narrow down the specific plugin causing problems.
If it's not a plugin (it's still "wrong" even with all plugins disabled), then leave your plugins disabled, and switch themes. Test again. If it's still "wrong", then your site is hacked - clean up the hack. If it's "right", then your theme is the culprit.

Need to disable TinyMCE default create an anchor tag when pasting a raw URL

I've got TinyMCE installed on frontend of my wordpress website…it's separate from the default Wordpress version, however, I need it to NOT turn raw URLs into anchor tags because it screws up Wordpress's ability to then oEmbed it. I have it installed on a form where users can submit articles in the frontend (not Dashboard). Is there a way to disable this? I thought convert_urls: false would to the job, but it's not meant for that. I've looked for over two hours now and cannot find a suitable answer. Thanks for the help.
well, usually it is not such a good practice to include 2 versions of the same script ..
at any rate -
I do not know specifically about TinyMCE, but you can use str_replace () php function (or preg_replace() ) to repair just about anything (if it is a string ..)

Drupal - Blog post links not working from certain points

we've put a quick Drupal 6 site as part of a Give-Camp and at the last minute noticed a problem with the site blog's recent posts section. If you go here: http://closingthenolagap.org/ you can see the problem that I'm talking about under Recent Posts. Those links should exist since those posts do.
We can't figure out what setting we messed up that caused this functionality. Any help would be appreciated.
See this short screencast for a further demonstration of the problem: http://screencast.com/t/Zjc3MWMwMD
Looks like you might have removed some (all?) of the character replacement rules from the pathauto module (I assume you're using - if not we could do with more info on what modules you do have).
The paths to your blogpost have a lot of URL-encoded characters like %20 (which is a space) - pathauto should be replacing these automatically with dashes or underscores, or removing them.

Resources