-
Feb 21, 2009
Want underscores instead of dashes in your permalinks in wordpress?
I wanted to add a filter to my wordpress theme to make Wordpress's URIs use underscores(_) instead of dashes(-) in it's page titles. I should be able to do the following:
<?php /*
Underscores instead of dashes please
*/
add_filter('sanitize_title_with_dashes', 'sanitize_title_with_underscores', 1, 1);
function sanitize_title_with_underscores($title) {
$title = strip_tags($title);
// Preserve escaped octets.
$title = preg_replace('|%([a-fA-F0-9][a-fA-F0-9])|', '---$1---', $title);
// Remove percent signs that are not part of an octet.
$title = str_replace('%', '', $title);
// Restore octets.
$title = preg_replace('|---([a-fA-F0-9][a-fA-F0-9])---|', '%$1', $title);
$title = remove_accents($title);
if (seems_utf8($title)) {
if (function_exists('mb_strtolower')) {
$title = mb_strtolower($title, 'UTF-8');
}
$title = utf8_uri_encode($title, 200);
}
$title = strtolower($title);
$title = preg_replace('/&.+?;/', '', $title); // kill entities
$title = preg_replace('/[^%a-z0-9 _-]/', '', $title);
$title = preg_replace('/s+/', '_', $title);
$title = preg_replace('|-+|', '_', $title);
$title = trim($title, '_');
return $title;
} ?>But nooooooo, it is not on the list of allowed functions you can override.
So instead you need to open yoursite.com/wp-includes/formatting.php and edit the sanitize_title_with_dashes function like so:
<?php function sanitize_title_with_dashes($title) {
$title = strip_tags($title);
// Preserve escaped octets.
$title = preg_replace('|%([a-fA-F0-9][a-fA-F0-9])|', '---$1---', $title);
// Remove percent signs that are not part of an octet.
$title = str_replace('%', '', $title);
// Restore octets.
$title = preg_replace('|---([a-fA-F0-9][a-fA-F0-9])---|', '%$1', $title);
$title = remove_accents($title);
if (seems_utf8($title)) {
if (function_exists('mb_strtolower')) {
$title = mb_strtolower($title, 'UTF-8');
}
$title = utf8_uri_encode($title, 200);
}
$title = strtolower($title);
$title = preg_replace('/&.+?;/', '', $title); // kill entities
$title = preg_replace('/[^%a-z0-9 _-]/', '', $title);
$title = preg_replace('/s+/', '_', $title);
$title = preg_replace('|-+|', '_', $title);
$title = trim($title, '_');
return $title;
} ?>You really only need to adit the following three lines:
<?php $title = preg_replace('/s+/', '-', $title);
$title = preg_replace('|-+|', '-', $title);
$title = trim($title, '-'); ?>to
<?php $title = preg_replace('/s+/', '_', $title);
$title = preg_replace('|-+|', '_', $title);
$title = trim($title, '_'); ?>If you would like your admin panel to reflect this change, you need to open yoursite.com/wp-admin/options-permalink.php and find "_e('Common settings')" (currently starts on line 147 in version 2.7.1) and change "sample-post" to "sample_post" in table class="form-table".
Unfortunately you will have to make this change every time you upgrade your wordpress install until they allow you to add a filter to the dash function.
Posted by Haveboard on 02/21/2009 3:04 AM in Bitching, Geekery, Future, Websites