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.
As per @SinisterBeard’s very valid comment to the question already a couple of years back now, this answer has long been outdated and the mentioned function(s) hence been replaced by a newer API:
See wp_unique_post_slug
.
Original
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.