3

I need random (and unique) alphanumeric permalinks. I've looked at several plugins but they all seem to do some form of redirection to a longer URL. That's not what I want, I want the permalink to be changed.

Basically, I'm wanting URLs like bit.ly or 9gag.com.

I've looked, but came up empty in my searches - does a plugin exist that does this? If not, could someone point me into the right direction as to how I might accomplish it? I'm pretty self sufficient in PHP but I'm not very familiar with WordPress yet.

The easiest way I could think of is something like add_filter("post_slug", md5(rand())) (if that were actually valid code). Obviously, I'd have to check for duplicate slugs and what not...

Honestly, what I'd much rather prefer is something like: example.com/prefix/hf434g5ay/postname In that the /prefix/alphanumeric-key is required but postname is used for SEO.

2 Answers 2

5

If you'd like to keep your slugs for SEO, then you'll want to define a new rewrite tag and leave the default behavior for post slugs.

If you'd like a unique id, then instead of looking up possible duplicates, why not just re-use the post ID which is guaranteed to be unique given a MySQL primary index – you can convert it to a base36 number if you want it to look more like bitly.

add_action( 'init', function() {
    add_rewrite_tag( '%my_id%', '([a-z0-9]+)' );
});

add_action( 'pre_get_posts', function( $query ) {
    if ( ! $query->is_main_query() || is_admin() )
        return;

    $id = $query->get( 'my_id' );
    if ( ! empty( $id ) ) {
        $query->set( 'p', base_convert( $id, 36, 10 ) );
        $query->set( 'name', null );
    }
});

add_filter( 'post_link', function( $permalink, $post ) {
    $id = base_convert( $post->ID, 10, 36 );
    return str_replace( '%my_id%', $id, $permalink );
}, 10, 2 );

Then change your permalinks structure to /%my_id%/%postname%/ in Settings - Permalinks. If you want more than 0-9 and lowercase characters, you can look for some base 62 implementations, though I'm not a big fan of case-sensitive URLs.

Hope that helps.

1

To append a random slug to a post using something like:

add_filter( 'wp_unique_post_slug', 'unique_slug_108286', 10, 4 );

function unique_slug_108286( $slug) {    
    $slug = mt_rand(); //just an example
    return $slug;
}

I'm pretty sure WP will handle duplicates using this filter (it does by default) so you do not need to check for them yourself.

You can add a prefix manually in your permalinks admin setting by setting it to something like /prefix/%postname%/

It might be better to use a category or custom taxonomy for the prefix. As for setting the random slug before the postname like prefix/hf434g5ay/postname I think it would require using the http://codex.wordpress.org/Rewrite_API , it would be easier to create a custom post type to do this instead of mangling the default "posts".

4
  • Thanks, that filter got me started but unfortunately it is applied after the check for duplicates. `wp-includes\post.php:3084' is where it's applied. Jul 30, 2013 at 16:14
  • Hmm that is good to know and also bad news.
    – Wyck
    Jul 30, 2013 at 16:44
  • Either way, I was able to simply copy part of the WP native code into my filter to check for uniquness. Plus, the slugs I'm generating aren't really random - they're based off of the post_id and a few other static strings to create a min length. Very doubtful that they'll ever not be unique. Anyway, I got the prefix/alphanumeric-key URL scheme working. That's all I care about for now. Thanks Jul 30, 2013 at 17:58
  • 1
    You should post your solution as an answer to close this thread.
    – Wyck
    Jul 30, 2013 at 18:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.