Integrating bit.ly with WordPress 3.0 Shortlinks Support

The official release of WordPress 3.0 being a bit recent, I’ve had a hell of a time trying to find bit.ly plugin that supports WordPress 3.0. The idea is to get your bit.ly short URL instead of the WordPress 3.0 short URL that uses your own domain name when you press the “Get Shortlink” button of a post editor.

bit.ly support in WordPress 3.0

I found the “WP Bit.ly” plugin which looks pretty good but does not yet support WordPress 3.0.

So what does an unsatisfied geek do on a Friday evening instead of having dinner? He finds a solution.

1. Download the WP Bit.ly plugin

2. Fix the plugin code

In file wp.bitly.php, put lines 47-48 in comment:

/*add_action( 'wp',      'wpbitly_shortlink_header' );
add_action( 'wp_head', 'wpbitly_shortlink_wp_head' ); */

If you too are a geek and you need to know, this will make sure WordPress 3.0 continues to take care itself of the HTML response header and the Web page HTML header as per the rel=”shortlink” proposed standard. Otherwise, the WP Bit.ly plugin would add these twice to a page which wouldn’t be clean.

3. Modify your theme’s function.php

Add the following code to your theme’s functions.php file:

/* bit.ly shortlink */

function get_bitly_shortlink($shortlink, $id, $context, $allow_slugs) {
   if (function_exists('wpbitly_get_shortlink')) {
      $slink = wpbitly_get_shortlink($id);
      if (empty($slink)) {
         wpbitly_generate_shortlink($id);
         $slink = wpbitly_get_shortlink($id);
         }
      return !empty($slink) ? $slink : $shortlink;
      }
   return $shortlink;
}
add_filter( 'get_shortlink', 'get_bitly_shortlink',10,4);

This code links the plugin code to the new WP 3.0 shortlink feature. It also makes sure that WordPress 3.0 will continue to operate as before if you do not enable the WP bit.ly plugin or you disable it.

Generate the bit.ly links from the plugin

So that the “Get Shortlink” button gets you a bit.ly link (instead of the default permalink), these should be generated once from the plugin’s settings. For new posts new bit.ly links should be created automatically.

Be aware that visits and crawls to your site will generate bit.ly links for each visited posts if no link already existed for each visited post. This is due to the fact that the short links are included in the code of each of your page to support new standards.

8 thoughts on “Integrating bit.ly with WordPress 3.0 Shortlinks Support

  1. Totally appreciate the article, I’ve been a little behind on updating my plugins as of late; work and paying clients have to come before my fun, after all.

    0.2.4 has fixed the lack of wp3.0 support and even includes backwards compatibility for those who are slower to upgrade (if you are one of these people, upgrade! 3.0 is impressive, to say the least!).

    As mentioned in the forums, if you have any suggestions for improving WP Bit.ly I’m all ears.

    • Hi, I’m having a lot of error in my error log:

      [Sun Aug 08 12:21:09 2010] [error] [client 67.195.xxx.xxx] PHP Warning: Missing argument 3 for wpbitly_get_shortlink(), called in /var/www/vhosts/domain.com/httpdocs/wp-con
      tent/themes/twentyten/functions.php on line 126 and defined in /var/www/vhosts/domain.com/httpdocs/wp-content/plugins/wp-bitly/wp-bitly.php on line 192

      Argument 2 is missing too.
      Arguments from de wp-bitly.php are:

      function wpbitly_get_shortlink( $shortlink, $id, $context )

      I think my implementation, recomended by Jerome, is missing $id & $context.

      MArk, Jerome: ANy idea?
      Thanks.

  2. I can’t find those 2 lines in my wp-bitly.php file. I can see the lines that remove the action of the wordpress’s core function but I can’t find the lines in the plugin file.

  3. Sorry about the spam but I’ve found the lines in another file: “deprecated.php”. I don’t know if it’s working the same way…

  4. Working now!
    Just did it in my deprecated.php file

  5. Working fine for me, now I want to use some ReTweet Button using the bit.ly shortlink but I cant’t figure out how to echo it.
    Would you please help this amateur?
    Thanks.

  6. I can’t find those 2 lines in my wp-bitly.php file. I can see the lines that remove the action of the wordpress’s core function but I can’t find the lines in the plugin.

  7. I am also having a hard time finding that code in the php file… guess I will keep digging?

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>