Codex

Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

User:Hearvox/Naming Slugs

WordPress is filled with slugs. Not the gastropod mollusks. More like in newspapers where every article gets its own "slug," a short identifying phrase -- often an abbreviated version of the headline, like "DEWEY BEATS TRUMAN". That phrase is unique; no other article gets that same slug. People, and computers, can use the slug to refer to that article and no other.

WordPress assigns an identifying slug to every post (and taxonomy term). That slug becomes part of the post's Permalink URL. This article details the WordPress items that gets slugs, how to edit slugs, why WordPress sometimes adds "-2" to your slug name, and rare but possible permalink conflicts (a.k.a, "collisions") between post and taxonomy slugs.

What Gets a Slug

Every instance of these post types gets a slug (URL query parameter(s) shown in parentheses):

Every instance of these taxonomies also gets a slug (URL-query parameter in parentheses):

http://example.com/?name=random-tape-david-weinberg http://example.com/?pagename=about-transom http://example.com/?name=about-transom http://example.com/?post_type=attachment&name=first_day_school

http://example.com/?pagename=belgrade-elementary-school-new

http://example.com/?taxonomy=project-types&term=assessment

http://example.com/?tag=apple

What's In a Slug

WordPress makes a slug from the post's title. Slugs use only lowercase alphanumeric characters, underscore, and dash ([a-z], [0-9], [_], [-]). When generating a slug from a title, WordPress does this:

  • Converts accented letters to their unaccented equivalents.
  • Converts uppercase letters to lowercase.
  • Removes punctuation and symbols, except underscore and dash.
  • Replaces whitespace(s) with a dash.
  • Removes leading or trailing dash.
If this were the (admittedly awkward) post title:
Here's Lookin' @ You, Niño #Casablanca 1942-11_26 :-)
This would be the WordPress-generated slug:
heres-lookin-you-nino-casablanca-1942-11_26

??? (for permalinks and no-permalinks): While your post/Page still has a Status of Draft, any changes you make to the title, will also change the slug. Once the post is Published, editing the title does not alter the slug.

The slug becomes the last part of your post's permalink URL. You my wish slug part of your permalink to be different than the title, perhaps shorter. To keep the same title but customize the slug:

  • Click the Edit button; this reveals a text input field.
  • Type in the text of your custom slug.
  • Click the OK button.
  • Click the Update button to save your new slug (or Save Draft if the post is not yet published).

Unique Slug Names (Why "-2"?)

All top-level parent page slugs (Pages and CPTs) must be unique. All Page slugs (and hierarchical CPT page slugs) with the same parent page must be unique. Attachment slugs must be unique among all post types (including nav_menu).

Attachment slugs must be unique slugs

Taxonomy Slugs

Permalink Rewrite Bases

Source Files


register_post_type and register_taxonomy uses sanitize_title_with_dashes

clean_term_cache

save for possible use:

Edit Screen

Search Engine Optimization

Introduction_to_Blogging#Pretty_Permalinks

images and other file attachments

term gets parent (if hier.) then -2


returned value is intended to be suitable for use in a URL