3

I would like to use underscore in my permalinks instead of hyphen.

Current permalink:

www.example.com/2013/01/hello-this-is-a-test-post/

Desired permalink

www.example.com/2013/01/hello_this_is_a_test_post/

I have tried some solutions mentioned here in stacexchange. But they were not working.

  • 1
    what about writing a script to change the slug of every post? – Mridul Aggarwal Jan 5 '13 at 16:14
  • Dashes seem to be the more reliable word separator from what I can tell. Why are you doing this? – s_ha_dum Jan 5 '13 at 16:24
  • 1
    Which some solutions? And note Google treats - as word separator, but not _. You will hurt yourself. :) – fuxia Jan 5 '13 at 16:24
  • @toscho I've tried this one. If google doesn't treat underscore as word separator, why does some popular sites use it?. Example wikipedia link. Example wordpress codex link – Giri Jan 5 '13 at 16:41
  • @s_ha_dum underscore looks more pretty than hyphen. Example wikipedia link – Giri Jan 5 '13 at 16:44
1

it can be done easily without any codes or plugin just go to pages dashboard, click Quick Edit" which url you want to change. Now add underscore. Update and done

| improve this answer | |
0

Toscho advised me like this.

Google treats - as word separator, but not _. You will hurt yourself. :)

He was 100% true. Here is an article that explains it. So I dropped the idea.

But if you still looking for a solution here is the answer.

Answered by this stackoverflow user

Hunt down the following file: wp-includes/formatting.php

Jump down to the sanitize_title_with_dashes function. You'll find this section of code inside:

$title = strtolower($title);
$title = preg_replace('/&.+?;/', '', $title); // kill entities
$title = str_replace('.', '-', $title);
$title = preg_replace('/[^%a-z0-9 _-]/', '', $title);
$title = preg_replace('/\s+/', '-', $title);
$title = preg_replace('|-+|', '-', $title);
$title = trim($title, '-');

Swap out all of the dashes/hyphens (-) for underscores (_) like so:

$title = strtolower($title);
$title = preg_replace('/&.+?;/', '', $title); // kill entities
$title = str_replace('.', '_', $title);
$title = preg_replace('/[^%a-z0-9 _-]/', '', $title);
$title = preg_replace('/\s+/', '_', $title);
$title = preg_replace('|-+|', '_', $title);
$title = trim($title, '_');

Note that any posts you've created before this change, and rely on the %postname% permalink structure tag, will be broken.

In that case you'll need to go back and republish those post so the dashes are swapped out for the underscores. Or just write yourself a little SQL to replace them.

| improve this answer | |
  • I don't get it, then why would wikipedia use underscores?? and I'm using farsi letter in my url, comapre یک-پست with یک_پست! I definitely like underscore here. but I need to confirm underscores are bad for seo – azerafati Aug 8 '15 at 4:39
  • I got this finally, webmasters.stackexchange.com/a/445 – azerafati Aug 8 '15 at 4:45
0

I initially did this but with every update to Wordpress I'd have to make the manual change again, so I made this plugin in case it helps anyone: http://wordpress.org/plugins/underscores-in-permalinks/

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.