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.
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.
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.
To append a random slug to a post using something like:
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”.