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.

Is there a page somewhere that details exactly how WordPress generates slugs for URLs? I'm writing a script that needs to generate URL slugs identical to the ones WordPress generates.

share|improve this question

2 Answers 2

up vote 3 down vote accepted

Off the bat, I can't give you a page/tutorial/documentation on how WP slugs are generated, but take a look at the sanitize_title() function.

Don't get a wrong impression by the function name, it is not meant to sanitize a title for further usage as a page/post title. It takes a title string and returns it to be used in a URL:

  • strips HTML & PHP
  • strips special chars
  • converts all characters to lowercaps
  • replaces whitespaces, underscores and periods by hyphens/dashes
  • reduces multiple consecutive dashes to one

There might be edge cases where the core does something additional (you'd have to look at the source to verify that sanitize_title() will always suffice in generating exactly the same you expect), but this should cover at least 99%, if not all, cases.

share|improve this answer
    
+1 Seems like I misunderstood the question slightly. –  kaiser Nov 30 '12 at 17:05
1  
@kaiser: I figured you're simply in love with T5 Rewrite :) (which is on my agenda to check out) | and generally: If a title is really long, WP also trims slugs to a certain maximum length. That's one of those cases where I am not entirely sure whether that's done by the sanitize_title filter applied in the function as well, or whether that's handled by something else after the function has run. –  Johannes Pille Nov 30 '12 at 18:24
    
I guess it's the sanitize_title-filter that one needs to check out. But there're also other related functions. I really got no idea about this. Sometimes it seems SimplePie is easier to understand than the WP_Rewrite thing. Btw: Toscho works on integrating the "Monkeyman Rewrite Analyzer"-functionality into his plugin. That's where things start to really get interesting. –  kaiser Nov 30 '12 at 18:52

Core at your service

There's no developer mode built into WordPress aside from WP_DEBUG, which doesn't help you too much in this case. Basically WP uses the "Rewrite API", which is a function based, low level wrapper for the WP_Rewrite class, which you can read about in Codex. The global $wp_rewrite object stands at your service to inspect it or interact with the class.

Plugins that help looking into it.

Toschos "T5 Rewrite"-Plugin and Jan Fabrys "Monkeyman Rewrite Analyzer"-Plugin will guide you your way. I've written a small extension for "T5 Rewrite" to smoothly integrate it with the "Monkeyman Rewrite Analyzer", which you can find in the "T5 Rewrite" repos wikie here on GitHub.

The "Monkeyman"-plugin adds a new page, filed in the admin UI menu under Tools. The "T5 Rewrite"-plugin adds a new help tab to the Settings > Permalinks page. My extension adds the help tabs to the mentioned Tools-page too.

Here's a screenshot of what the "T5 Rewrite"-plugins help tab content looks like.

enter image description here

Vorlage = Pattern | Beschreibung = Explanation | Beispiele = Examples

Notes

The "T5 Rewrite"-plugin does a wonderful job with helping you inspect the rewrite object. And it does even more: It adds new possibilities. Therefore it's (at least in my installations) part of my basic plugins package.

share|improve this answer

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.