Farewell Blogger.. Hello WordPress

My blog (and website) is now running on self-hosted WordPress installation. Yeah, it’s a PHP-thing, but I couldn’t bother making my own with Django or similar using Python. No time for that and I’m not a programming-language-radical-idiot.

RSS feeds have changed: you’ll find the link on my new website.

Why I moved away from Blogger? It just plain sucked, and I have had my fill of it..

I needed to create some plugin for WordPress. Existing plugins redirecting old Blogger URLs were not good enough. The code you’ll find below will redirect links from Blogger to the new links in WordPress. Hopefully it helps somebody!

/*
Plugin Name: Redirect Blogger links
Version: 1.0
Plugin URI: http://geert.vanderkelen.org/post/301/
Description: Redirect requests using Blogger links to Wordpress link.
Author: Geert Vanderkelen
Author URI: http://geert.vanderkelen.org/
*/

add_action('init', 'redirect_blogger_links');

function redirect_blogger_links()
{
  global $wpdb;
  $uri = $_SERVER['REQUEST_URI'];

  $matches = array();
  if( preg_match('#(/[0-9]{4}/[0-9]{2}/.*\.html)$#', $uri, $m) ) {
    $query = "SELECT post_id FROM $wpdb->postmeta ".
      "WHERE meta_key='blogger_permalink' AND meta_value='".$m[1]."'";
    $post = $wpdb->get_var($query);
    if( $post ) {
      $wp->query_vars = array('p'=>$post);
      $url = get_permalink($post);
    }
    if( isset($url) ) {
      header("Location: $url", true, 301);
      header("Connection: close");
      exit();
    }
  }
}

Note: if somebody can optimize it so the plugin only kicks in on links from Blogger, that would be great. It’s now executed on each request, which is a bit overkill.