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 have done a lot of googling on this so I sincerely apologize if there is a codex entry for this but for the life of me I can not find a hard and fast rule about what characters are acceptable in a "slug" ( any slug really, but more specifically for a custom post type in this instance).

Presently we have a situation where we have a few custom post types with a slug structure like (pseudo) :

some_Context_Thats_Not_A_Category_slug/sub_context_thats_not_a_category_slug/ + the post title

The segment in grey is the "slug" in it's entirety. In practice this would look like say :

hamburgers/cheeseburgers as the post type slug

Is the forward slash use allowed / safe in this scenario?

Criteria for an answer to be accepted:

"yes" or "no" to the question posed and a link to where I can find contemporary WP slug rulesets.

Thanks in advance!

share|improve this question

1 Answer 1

up vote 6 down vote accepted

No, forward slashes are not allowed to be used in slugs. However, they are automatically stripped out when trying to include slashes in a slug on the post editing screen. Slugs are sanitized by using sanitize_title().

Unfortunately, there is no easy way to explain what is and isn't allowed in a slug, let alone to give a simple set of rules to which valid slugs must adhere. However, we can summarise the most important characteristics of a slug quite compactly: a slug consists of solely lowercase alphanumeric characters, dashes and underscores, without 2 or more dashes in a row (sequences of underscores are allowed). Furthermore, a slug cannot start or end with a hyphen.

sanitize_title()

Parameters:

  • $title: Title to be sanitized
  • $fallback_title (optional, defaults to empty string): Slug to use if the sanitized $title turns out empty
  • $content (optional, defaults to 'save'): The operation for which the string is sanitized

The function sanitize_title behaves in the following way (by default, with default $fallback_title and $context arguments, with no external filters applied):

  1. Accented characters such as "é" and "Ö" are replaced by their corresponding "unaccented" characters (in this example, "e" and "O", respectively)
  2. sanitize_title_with_dashes() is called on the resulting string

The longer (and more complete) explanation can be found below.

sanitize_title_with_dashes()

Besides the $title parameter, sanitize_title_with_dashes() has two more arguments, the first of which isn't used, and second of which is the content, $context, which is passed from sanitize_title (and is thus 'save'). sanitize_title_with_dashes() does a lot of things, so I'll try to explain what sequence of steps it performs to sanitize a string.

  1. All HTML tags are stripped
  2. Percentage signs are removed, except for the ones in octets (such as %20 for space)
  3. The string is converted to lowercase
  4. All HTML entities, such as & are stripped
  5. All dots (.) are replaced by hyphens (-)
  6. Both en dashes and em dashes (WikiPedia on dashes), as well as spaces, are converted to hyphens
  7. Some special characters, such as accents (e.g. the grave accent) are stripped
  8. The "times" character (×) is replaced by "x"
  9. All characters except for alphanumerical characters, spaces, hyphens and underscores are stripped
  10. Sequences of hyphens (2 or more hyphens in a row) are replaced by a single hyphen
  11. Leading and trailing hyphens are stripped

And there you have it, the full title-sanitization process!

share|improve this answer
1  
+1 Very comprehensive answer. –  kaiser Jun 10 at 20:50
    
engelen - many thanks couldn't ask for a more upfront and clear answer. Thank you. –  Brandt Solovij Jun 10 at 22:26
    
No problem, I'm glad too help! –  engelen Jun 11 at 8:14

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.