How I Set up URL Shortener With Yourls

I’ve had a URL shortener set up for a while. it was Not the best starting point for a URL shortener. So I decided to start over. No worries, I have a bunch of URLs all over the web using the old method, those will stay in effect. I’ll keep the old service in effect for now, I may eventually just use some redirects for it, but either way the URLs will be good. Anyway, I just set up the cool new service. For now it’s front facing, we’ll see if it gets abused. Let’s check out what I did to get working.

The very first thing you need to do is come up with your domain name. You can do like I did at first, and use a subdomain off of your main one. But you’ll probably get sick of that pretty soon. Short URLs are less than ideal if they are long! Domainr is a useful tool if you are trying to come up with clever names. I think for obvious reasons I started with Vudu. I was looking for voodoo, but its taken for all the normal extensions. It was available for some more exotic extensions, but they cost a lot more. I could get a .me extension through godaddy for like $8 a year.

That brings us to the next step, after you have a domain picked out you need to actually buy it. It gets harder and harder to find good domains as they are all bought up. You can use godaddy of course or another registrar to look for your names. One of the best places I found for seeing what’s available is Gandi. You can search pretty much all the domain extensions at the same time.

OK, once you have your domain bought, get it routed to your hosting. I’m not going to try to provide instructions here. How this is done depends on whether you have your domains and hosting through the same place (as I do with godaddy, for now), or who each of those companies is. You should be able to get help from them on how to get this all going.

Alright, I have my domian pointed to my vudu folder on my server. Next up, software. Grab the Yourls URL shortener package here. The instructions on the site are easy enough to follow. I had it set up within an hour, from the idea to the domain to the service. It’s really just a matter of setting up a mysql database, copying the config file and renaming it, making edits to the config file, and running the install (pretty similar to a WordPress manual install).

OK, now for some bonus info! Here’s what I did so far with my yourls. I first installed a plugin to use my new shortlinks instead of the built in WP version. There’s options here. You can grab this plugin from WordPress and install it. It will shorten all existing and future URLs for ya. It also has some twitter integration if you like to tweet your new shortlinks. Personally I already use the STC plugin for twitter integration, so I didn’t need the extra stuff. Luckily, neither did Otto. Here’s a version of that plugin with the fluff removed. It just takes care of making the shortlinks. It works perfectly here on VoodooPress.

Next up, I wanted to display the shortlink for each post on the single view. I did this from my functions.php file. You may recall a while back we had a couple of different posts about adding stuff to the end of each post from functions.php? Each time we made a little function, and tacked it into twentyten_posted_in. Well this is no different.

Here’s the function for displaying the shortlinks:

if ( ! function_exists( 'vudu_shortlink' ) ) :
function vudu_shortlink() { ?>
	<div class="vudu-shortlink"><span class="post-shortlink">Shortlink: 
	<input type='text' value='<?php echo wp_get_shortlink(get_the_ID()); ?>' onclick='this.focus();;' />
<?php }

Now you can just call up vudu_shortlink wherever you want your’s displayed. I have mine in a nice box so it can be selected with a single click. Here’s my twentyten_posted_in function from my child theme, notice it has three custom function in it, all from the previous linked tutorials.

function twentyten_posted_in() {
	// Retrieves tag list of current post, separated by commas.
	$tag_list = get_the_tag_list( '', ', ' );
	if ( $tag_list ) {
		$posted_in = __( 'This entry was posted in %1$s and tagged %2$s. Bookmark the <a href="%3$s" title="Permalink to %4$s" rel="bookmark">permalink</a>.', 'twentyten' );
	} elseif ( is_object_in_taxonomy( get_post_type(), 'category' ) ) {
		$posted_in = __( 'This entry was posted in %1$s. Bookmark the <a href="%3$s" title="Permalink to %4$s" rel="bookmark">permalink</a>.', 'twentyten' );
	} else {
		$posted_in = __( 'Bookmark the <a href="%3$s" title="Permalink to %4$s" rel="bookmark">permalink</a>.', 'twentyten' );
	// Prints the string, replacing the placeholders.
		get_the_category_list( ', ' ),
		the_title_attribute( 'echo=0' )

That’s it for displaying the shortlinks in the single post view. Just a couple more things I needed to set up to be happy. I visited this link for a quick theme to download for the front end of Looks good enough, I don’t expect it to be used much so I’m not too worried about the interface. I may get bored and re-work it all Voodoo Empire style.

And finally, just in case it does get a bit of action, we need to try to limit the bad action. Check out this post for some tips on preventing abuse of your shortener.

And that is as far as I’ve gotten right now! If you have any tips or tricks send em my way!

One thought on “How I Set up URL Shortener With Yourls”

Leave a Reply