How to prevent apostrophes and quotes from appearing in permalinks?

In WordPress, “—” and ” — ” become em-dashes (— —) and “–” becomes an en-dash (— #8212;). The sanitize_title_with_dashes() function doesn’t catch these.

That function uses the databased copy, but the title displayed to the user always goes through a texturize function. So if we replace en/em dashes on their way into the database, the net result will be the same and avoid these bad URL cases the titles are re-texturized.

add_action( 'title_save_pre', 'do_replace_dashes' );
function do_replace_dashes($string_to_clean) {
    # The html entities (–  and —) don’t actually work but I include them for kicks and giggles. 
    $string_to_clean = str_replace( array('—', '—', '–', '–', '‚', '„', '“', '”', '’', '‘', '…'), array(' -- ',' -- ', '--','--', ',', ',,', '"', '"', "'", "'", '...'), $string_to_clean );
    return $string_to_clean;
}

Leave a Comment