WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

sanitize_title_with_dashes (see code below for reference) is the function Wordpress uses to format "pretty" urls. However, contrary to the function's comment header, it allows much more than alphanumeric characters, underscore (_) and dash (-). It also allows signs like °, etc.

How would I go about really allowing only alphanumeric characters and dashes?

 * Sanitizes title, replacing whitespace with dashes.
 * Limits the output to alphanumeric characters, underscore (_) and dash (-).
 * Whitespace becomes a dash.
 * @since 1.2.0
 * @param string $title The title to be sanitized.
 * @return string The sanitized title.
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 = str_replace('.', '-', $title);
    $title = preg_replace('/[^%a-z0-9 _-]/', '', $title);
    $title = preg_replace('/\s+/', '-', $title);
    $title = preg_replace('|-+|', '-', $title);
    $title = trim($title, '-');

    return $title;
share|improve this question
up vote 1 down vote accepted

Consider this function as a rough placeholder. It has more flaws than you might imagine … :)
There are many plugins to improve the conversion for different languages and needs. You may take a look at my plugin Germanix to see how this could be done.

share|improve this answer
Thanks! I just had a look at your plugin, indeed there are a lot of language-specific tweaks, though French's probably slightly easier in this respect ;) What seems strange to me, though, is that I've never seen a Wordpress-generated URL in the wild with °, , etc. characters in them. Do most Wordpress use a more restrictive plugin, then? Or do people just rewrite all their URLs by hand? Mostly, I just want to remove all characters that are not a-z0-9-` (like StackOverflow seems to be doing in their URLs too, incidentally). I'll guess I'll just end up rewriting my own funct., but it sucks – julien_c Apr 22 '11 at 17:13
Plus, the function's documentation is just plain wrong: Limits the output to alphanumeric characters, underscore (_) and dash (-). Whitespace becomes a dash.. I don't get it. – julien_c Apr 22 '11 at 17:20
@julien_c In my plugin I run preg_replace('~([^a-z\d_.-])~', '', $str) after the conversion to eliminate such characters. In a short test ° seems to work in a URL. But yes, it is ugly. – toscho Apr 22 '11 at 17:29

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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