DiggingIntoWordPress

by Chris Coyier & Jeff Starr

Smarter Slugs ~!@$%^&*()={}<>[]?

Posted by on

See those crazy characters in the title of this post? Now see how they don’t appear in the post’s URL? That’s one of the finer details of the WordPress 3.3 update: smarter permalink slugs.

So when you type something like you see in the title of this post, with all the funky characters, or even just something like a comma, apostrophe, or semi-colon, WordPress 3.3+ works the magic and automatically creates your post slug without the junk.

Details.

It may not seem like a big deal, but previous versions of WordPress would include those funky characters when auto-creating your permalink slugs. If you glance at URLs while surfing around WordPress-powered sites, keep an eye on the URL in the address bar. It’s common to see all sorts of non-alphanumeric stuff in there.

Does it matter? I think so, for numerous reasons:

  • Readability, consistency – simple alphanumeric URLs work great everywhere, no need to clutter them up with redundant information. For example, funky characters can choke URL-shortening services. Also: fewer characters, facilitates better comprehension.
  • Safer – certain characters such as `, ^, ", ~, #, %, |, \, <, >, ", ~, [, ], {, }, and the blank space are considered as unsafe and should not be included in the URI (ie., always need encoded). Including them may seem to work, but you’re introducing sort of unknown variable into the mix, a potential vulnerability*
  • SEO — do funky characters like blank spaces and percentage signs in the URL hurt your site’s SEO? Maybe not, but why put anything in there that isn’t a keyword?

So smarter auto-slugs in WordPress 3.3, another one of the finer details that improves the overall WP post-editing experience, and something you may not have noticed.

Bonus tip

Another sort of related “smarter-slugs” feature noticed while looking into it, is the automatic removal of the “-2” that WordPress automatically appends to the post slug when a duplicate is detected. So for example, say you’re working on a new post:

  1. Create a post with a slug such as “test-post
  2. Delete the post and send to the Trash
  3. Create another post with the same “test-post” slug
  4. WordPress detects the duplicate post in the database and appends a “-2”, like so: test-post-2 to the post slug
  5. Create yet another post with the same slug and WordPress will append a “-3”, and so on..

Nobody likes the “dash-twos” but they are required for the auto-creation of non-duplicate post slugs. What I just noticed with version 3.3 is that, once you empty the Trash, WordPress automagically removes the “-2” from the post slug, improving workflow to save you time. This may have changed in a previous version and I just hadn’t noticed, or it’s another one of the administrative refinements of WordPress 3.3.

Update: it looks like the -2 removal only applies to drafts and pending posts, not to posts that have already been published.. (see comment from Otto)

* More info on forbidden characters and blocking them

17 Responses

  1. I read something that’s a little concerning in your post. Let’s assume that I create test-post and then create test-post-2. I publish them both and then trash test-post. Are you saying that test-post-2 will automatically become test-post, potentially screwing up any links that have been published with the original permalink of test-post-2? Just wondering what impact this has.

    • Yeah that’s a good point. I haven’t looked that far into it, but logically that is the implication of the auto-removal of the -2.. I’ll investigate and report back.

  2. Keith Wyland January 25, 2012

    Dave Clements beat me to it! :)

    Thanks for the insight with this article, Jeff!

  3. I’m unable to reproduce your claims for the “Bonus tip” with published posts on a test site running trunk. As far as I know, WordPress doesn’t do this.

    Are you doing this with published posts, or with *drafts*? Bit of a difference there, I’d think. Published posts should never get their slug changed by any automatic process. You can manually change them, of course, but nothing I know of will retroactively “fix” slugs in this manner.

    • I’ve had such an experince several times and I’ve never been eable to reproduce what’s in the “Bonus tip” either. WordPress would not mess with published a post slug even though it was appeneded on “-2″ on publication.

    • I’m thinking yeah the post slugs that were changing were for drafts.. do you know if WP removes appended -2 for posts not yet published (e.g., drafts & pending posts)?

  4. I’m currently still on 3.1.4 (my current host doesn’t have the right version of MySQL for newer versions, I’m currently moving to a new one that does) and tested this post title and it didn’t include those characters in the slug — are you sure this is a 3.3 feature, or am I confused on what you’re talking about here?

    • Sounds correct.. WordPress removes the funky characters from auto-created post slugs..

      • So is that not what you’re talking about in this post? *confused*

        • No idea.. perhaps there’s confusion on the difference between post titles and post slugs..?

        • So I specified “Smarter Slugs ~!@$%^&*()={}[]?” as the title of a post in my version 3.1.4 of WordPress and the permalink slug that came up was smarter-slugs (well, smarter-slugs-2 when I went to test again to confirm I wasn’t going mad) — is this ignoring-of-characters not what you’re talking about as a new feature of 3.3, or am I doing the wrong thing?

        • Ah gotcha. Yeah older versions do it too, but not as well, according to the 3.3 release notes.

  5. Hi, just a question for you.

    Can add title to tag “more”, but not “Read more + post title”

    need “Read more” and title of the post is pop-up…

    Sorry for my english)
    From Russia with love!)

    • If I understand correctly, you’re looking for a way to add a title attribute to the “read more” link.. if so, here is one way to do it, with this in the WP loop:

      <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" class="more-link">Read more...</a>

      ..and then to avoid the duplicate read-more link, just do this with the content tag:

      <?php the_content(''); ?>

      ..or use the_excerpt() or whatever.

  6. Along with this latest update of wordpress, I use the “seo slugs” plugin of old to keep my url’s search engine friendly. It works great and everyone is happy.

  7. cool but I want to auto add -2 to post when duplicating post with mysql ?

  8. You know if smarter permalink slugs filter utf-8 characters too? like ¿, ½ and others

Comments are closed. Contact us with any critical information. Thank you!

Code is poetry