What's the best way to ensure that post and page slugs use underscores instead of hyphens, without needing to edit the slug manually (or updating the page manually)

Current permalink:


Desired permalink


One approach I've tried is to hook into sanitize_title, but this seems to only be called when the post is updated, so not very practical for a blog of several thousand post. Is there any way to force each post to update?

I need this to preserve compatibility as an old MovableType site is moved into WordPress - using .htaccess isn't really an option

2 Answers 2


IMO hooking into sanitize_title is probably the best way to go here. Check out this plugin by Mark Jaquith which does something similar: http://txfx.net/files/wordpress/periods-in-titles.phps

As for updating the old posts, I would just write a simple script that would generate sql to manually update everything. (code not tested)

function sanitize_title_with_underscores( $title ) {
    return str_replace( '-', '_', sanitize_title_with_dashes( $title ) );

$posts = get_posts( array( 'numberposts' => -1 ) );

foreach ( $posts as $post ) {
    global $wpdb;
    $id = absint( $post->ID );
    $name = sanitize_title_with_underscores( $post->post_name );
    print "\n" . "UPDATE $wpdb->posts SET post_name = '{$name}' WHERE ID = {$id};"

Probably not the most elegant solution, but for something that only needs to be done once, it should work rather well.


So, this is what I ended up with, after converting the slugs of the imported content, this is hooked into sanitize_links. The last 2 rules are to keep compatibility with the Movable Type permalink scheme being used (and it deals with more than just hyphens)

I'm sure it's not the most efficient, but it might help someone.

function convert_permalinks( $old_slug ){

global $post;

if (!get_post_type() == "post") {
    return $old_slug;

# replace hyphens with underscores
# replace apostrophes with spaces
#if last char is an underscore, remove it.
$search_values = array("-", "–","‘","'","’","“","”");
$replace_values = array("_","");

// if there are no hyphens then skip
if (! preg_match('/-/',$old_slug))
    return $old_slug;

$new_slug = str_replace( $search_values, $replace_values, urldecode($old_slug));

if (strlen($new_slug) > 30)
    $new_slug = trim(substr($new_slug,0,30));

if ($new_slug[strlen($new_slug)-1] == "_")
    $new_slug = substr($old_slug,0,strlen($new_slug)-1);

return $new_slug;


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.