Want to Write for Us?Read This | LoginBecome a Member

Auto-Generate a TinyURL Link for Every Post

tinyfrogContinuing my sem-series of posts that dissect some of what I’ve done on the recent redesign of ChurchCrunch, here’s how I auto-generate a TinyURL for each post automatically.

First, obviously, is why I do this. Here are some reasons:

  1. Some of my blog post titles are super long. Whoops. I want to be able to provide people with an easy-to copy URL for whatever purposes they might entertain.
  2. There are shorter services out there but none as well-known than TinyURL. I want to maximize potential clickthroughs for the people who use them by providing the most recognizable service.
  3. I copy and paste my links all the time. This helps do the work for me every single time without having to use a browser bar-addon or anything fancy like that.
  4. I thought it would be cool.

Here’s how you do it:

1. In your functions.php file in your theme template directory, add this code:

function getTinyUrl($url) {
$tinyurl = file_get_contents("http://tinyurl.com/api-create.php?url=".$url);
return $tinyurl;
}

2. Then in your single.php file or wherever the “loop” is, paste this

<?php $turl = getTinyUrl(get_permalink($post->ID)); echo '<a href="'.$turl.'">'.TinyURL.'</a>' ?>

Fun times!

[Completely random image from Bob]

7 Responses to “Auto-Generate a TinyURL Link for Every Post”

  1. April 24, 2009 at #

    nicce,
    love this one and the email one earlier

  2. April 24, 2009 at #

    very cool…thanks

  3. Jim
    April 24, 2009 at #

    super-ninja skills, thanks sensei!

  4. April 25, 2009 at #

    I have it on my main index page as well as my single index page. It looks like it's showing up, but I can't get the tiny url to actually generate. Take a look and give me some help if you can.

  5. April 27, 2009 at #

    Why not use short URLs that are easier to generate, easier to remember, and help your branding?

    http://churchcrunch.com?p=4891

Leave a Reply

Gravatar Image