Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm using custom permalinks "/%category%/%postname%/". Whenever a post title contains quotes or apostrophes they appear in the URL.

Can someone tell me how prevent them from appearing in the slug?

I am running WordPress 3.0.4.

share|improve this question

2 Answers 2

up vote 5 down vote accepted

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;
}
share|improve this answer
    
Could you not also call the texturize filter in place of the string replace? (i've not tried it, just curious if you thought of it and if so, why you decided against it).. –  t31os Feb 4 '11 at 0:17
1  
You could do that, but I see two problems. First, you're not stopping the bad permalinks from being created -- you're just hiding that fact as they come out of the database. That might have weird consequences. Second, your function will run every time wp_texturize runs. That's not a huge deal but you'll certainly save some cycles over time by calling it once as the perma goes into the database and not every time it comes out of it. –  editor Feb 4 '11 at 6:01

I've seen there are some plugins to work around this problem. Check Clean URL for example:

This simple WordPress plugin is used when generating article slug (= article name used in URL). It removes all characters other than letters a-z, numbers and hyphens (-). The plugin runs as the last one in the whole url-generating process, so first all standard replacements of diacritics and accents are performed, and then, if still some strange characters are present they are cut out.

share|improve this answer
    
I tried out the plug-in, but it unfortunately didn't work. I then tried the function that @editor posted above and it worked. –  user1462 Feb 3 '11 at 17:45
    
@user1462 Great! You should contact the developer and report a bug on the plugin with the info on your WordPress installation. –  Fernando Briano Feb 3 '11 at 19:20

Your Answer

 
discard

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.