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:
www.<domain>.com/2011/05/this-is-a-test-post/
Desired permalink
www.<domain>.com/2011/05/this_is_a_test_post/
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
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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)
<?php
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.
Method 2
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;
}
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0