Using YOURLS with WordPress
I recently acquired a short domain name, otto42.com (the otto.com domain is owned by a German company, and I use Otto42 as my alias a lot on sites as “Otto” is usually already taken). So I decided to use it to make my own URL shortener.
I looked at various methods for this, including simply writing my own (honestly, it ain’t that hard), but I wanted to see what was already available first.
First thing I looked at was the shortlinks system built into Google Apps. I’ve used it before, and it’s fairly nifty. They even have an API available for interfacing it to other things. And hey, the redirections would be served up by Google’s servers, which you can’t beat for reliability. Sadly, after screwing around with this for a while, and trying out some example code, I was totally unable to get their API to work. The whole thing is written in Python, which I have an intense dislike of (whitespace my ass!), and when trying to access the thing via direct calls, it gave me nothing but errors and annoyances.
So I searched around a bit more, and was relegated to building my own, when a tweet by @ozh mentioned that he’d put a blog up on yourls.org. This looked promising, as I know Ozh is active in WordPress, and so of course it would have a plugin. After investigating, I went with it. It works pretty well, actually.
How to do it
First thing I did was to point my short domain at a directory on my hosting account. If it’s going to run code, it needs to have a place to do it from. So I made a yourls directory and pointed the domain to it using my hosting control panel.
Next, I grabbed a copy of the yourls code and installed it there. 300+ files to FTP? Yikes. Complex. A quick bit of reading told me that it needed a database and a config file. Very similar to WordPress in this respect, except the config file resides in the includes subdirectory for some reason. Not so obvious, but whatever.
I toyed with the idea of using the same database for yourls that I already use for WordPress, but ended up creating another one for it. I’m now thinking that it might be worth merging them though, as that way, VaultPress would probably back up the yourls tables too.
After some hosting weirdness, the thing was up and running.
Plugin
Installing the WordPress plugin is a snap. Just find it in the Add New Plugin interface and hit install. After Network Activating it for all my sites, I went to configure it. I had to configure it on each site separately, but no big deal.
Configuration is fairly straightforward. Tell it URL of the API interface, give it my username and password, and voila. Worked first try.
There’s a bunch of Twitter stuff there as well which I only set up to make it stop bothering me about it. I don’t want this plugin to send out my tweets on new posts, as I already use my own Simple Twitter Connect for that. Still, it bugs you with an error message if you don’t set it up, so I did. Thankfully, there’s checkboxes to turn the autotweeting on and off, so I just leave these turned off. Honestly, I think this whole Twitter thing should be removed from the plugin, but I can understand the “easy” factor here for new users. Making them totally optional would be a nice enhancement though.
At first, I was confused because when I turned it on, I saw no way to make it back-populate my old posts into the shortener. Turns out I didn’t need to, it started doing it all by itself shortly afterwards. I think it actually does shortening on an on-demand basis, creating a shortlink whenever there isn’t one already there for a post.
The plugin also hooks into the Shortlink API, meaning that my Twitter plugin will automagically use the new shortlink. You can see that the shortlink box below this post has the otto42.com shortlink in it. I had to make zero changes to make that work. Isn’t interoperability fun?
One other thing I did have to do was to turn off the old wp.me shortlinks I used. These are provided by the WordPress.com Stats plugin, and there’s a checkbox in its options page to turn them off. No big deal.
Final thought
So yeah, if you have a short domain and want to make your own shortlinks, then YOURLS is a pretty good choice. I haven’t played with the stats gathering part of it yet, for the simple reason that I only just turned it on and thus have no stats to view. Still very easy to do, on the whole. Of course, if it could be entirely a WordPress plugin, then I might think it much cooler. 🙂
Update: There is a minor problem in the way the YOURLS plugin handles the WP Shortlink API. I’ve reported it upstream, hopefully it can be fixed soon. Still, it’s a minor issue. The workaround for now is to call wp_get_shortlink(get_the_ID()) when you want to get the shortlink for a post inside the Loop.
The thing is, twitter is planning to shorten all the links with t.co,
they’ll even start re-shortening the links later this year.
Although it might be cool, it also might be useless soon to have your own short-url.
Actually, they’re shortening them already. However, a) you still get your shortlink displayed on Twitter.com, so that’s nice and b) the redirect through YOURLS gives you stats and other useful information. Yes, it’s yet one more redirect. Big deal.
They’ll be redirecting everything through t.co, but it’s transparent, for the most part. Still useful to have your own shortlink for a lot of users.
Otto
It might be worth checking out, but there’s a way you can use a PHP plugin and the code listed at
http://mikebeach.org/2010/07/drupal-and-yourls/
to create a codebox that calls the Yourls API (even if it’s hosted on a different server)
I’ve been using YOURLS for quite some time on http://v007.me/ and I love it! 🙂
Just found a bug with the plugin, actually. It doesn’t handle the case where no ID is passed to the wp_get_shortlink function particularly well. Most plugins check with get_the_ID() to get the ID of the current post, but the yourls plugin gives back nothing for a zero ID. Thus, calling wp_get_shortlink() with no first parameter doesn’t return the shortlink for the current post when you’re inside the loop.
Yourls is a great tool for url shortening!
I’ve been using yourls with my domain p499.net for a little while now, and it works just as expected. The next part is not directly connected with the yourls server, but sort of relevant anyways.
I’ve been using the plugins yourls:wp to twitter and simple tags on wordpress. My problem is that it did look like the %H (tags-switch) did work perfectly for a short while then suddenly stopped working – now it only posts a single # after the title and short url which is rather useless. Anyone else noticed this? I find it rather handy to add tags to my posts as they’ll kinda drown otherwise. And as it worked for a first couple of posts, and then just stopped it doesn’t look like a problem with settings either.
when i set up yourls on my domain, inside a subfolder, i get /domain/yourls/shortlink as the yourls link.
if i’m reading this right, you’re saying you have yourls inside a subfolder of otto42.com. how are you able to get shortlinks like /otto42.com/shortlink?
cheers, and thanks for bringing yourls to my attention.
No, I have yourls installed in a subfolder of my hosting account, and I then pointed otto42.com to that folder as its root. So it’s at the root of that domain, but not of the general hosting account.
So if you have that installed in a sub directory do you think you could then install wordpress into the root level and have the site work of wordpress and still use YOURLS without showing a subdirectory domain.com/url/shorturl ?
I would also like to do this ~ if anyone has it figured out?~ may be difficult.. with the .htaccess taking over everything,,
I’m in exactly the same boat and would love to find a solution to this if anyone out there knows and could share.
why not something like o4.me? bit.ly/pro what about that? doesn’t that allow custom domain?
I use bit.ly pro for my shortlinks, does me fine!
Have you had any issues getting yourls to work properly with a multisite install? I’m not having any problems with my single site install but the multisite just give me a page not found rather that redirecting the correct link.
Nope. In fact, I’m using it on this site, which is a multisite install. Works fine across the two sites I activated it on.
Are you using a subdomain or a subfolder? I’m using subfolders and WordPress just seems to redirect it the base install as a sub directory.
I’m using subfolders with domain mapping. However, that’s pretty much irrelevant. Yourls is a separate piece of software. The WordPress plugin just interacts with it. The redirect is done before it ever gets anywhere near WordPress itself.
Then there is something wrong with my multisite install, as it must be the one highjacking he links. Unless I use the specific admin page URL everything else just bounces a 404.
Thanks for the help in getting me this far. I also appreciate the SUPER fast replies.
Thanks Otto, I re-read your comments again and I managed to figured it out. I had to re-edit my .htaccess file for the YOURLS install directory:
# BEGIN YOURLS
RewriteEngine On
RewriteBase /somedir/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /somedir/yourls-loader.php [L]
# END YOURLS
It’s the /somedir/ that is key. Both instances.
This helps me too. Thanks to Otto and Johnny!
Great article Otto. I’ve setup yourls and it’s working nicely. The only change I wanted was to have a random short url created each time instead of the sequential numeric default.
I was going to hack at the code but found out a plugin already exists for this. It’s called “Random Keywords” and you can find it here: http://code.google.com/p/yourls/wiki/PluginRandomKeywords
Great stuff.
~David
[…] Using YOURLs with WordPress – Otto has worked on this cool script called YOURLs that lets you create your own URL Shortner. Here is an article where he shows how you can use it with WordPress. […]
Sorry for digging up an old thread, but I too want to use this on the same site as a WordPress Network (multisite) installation. Not the wordpress plugin, per se, but install it on the same root directory as a wordpress network. I am very afraid that the either the Domain Mapping pluging or the wordpress permalink structure would conflict.
My network root is 4 characters plus a dot com, so the the shortest ever, but short enough. No one uses the root website other than me for network administration as of now. The other option is to create an additional url (then no worries, but don’t want to have to support another url forever for no reason) or just manually add redirects to by .htaccess whenever I want to create something short (wow would that be a pain). Or maybe there is other software that would work better for me.
Thoughts?
Thanks.
This site is a multisite, domain mapping, install with http://ottodestruct.com. I’m using this plugin on it, Network Activated. So, works for me. 🙂
However, using YOURLS itself on the same domain as an existing WP install would indeed be problematic, at best. It could be done with some wild-and-wacky .htaccess rules, but it’d be fragile.
You might look at using a WP plugin to provide shortlinks instead. Something like this perhaps: http://wordpress.org/extend/plugins/shorturl/
There’s others in the plugin directory too that might work.
Hi Otto,I installed YOURLS on wordpress multisite whit mapping plugin on and it is working, but I have a problem. I can’t set diffrent twitter addresses for each domain. Do you have an idee how I can do that?
Regards
[…] out her article for information beyond what I provided, then why not take the plunge? You can also check out Otto’s article, which was the one that pointed me towards Yourls. Custom URLs are what the cool kids are doing! […]
Ahoy there,
is there a way to embed Yourls or at least get it running within a wordpress page or site?
cheers,
ian
Hey there, I already tried to get help from the support forum but: I’d like to have the created shortlink displayed within the post somehow, for example in the section where author, date and categories are displayed – so people can easily grab the shortlink of the post from there. Is there any way to do so?
I am looking to install the script in sub domain with wordpress installation. how can i go about it?