Automatically post friendly URLs to Twitter and Facebook

January 12th, 2008 Jason Posted in Facebook, Twitter, geek, php, plugins, seo, site, wordpress | No Comments »

In a fit of self-propagation, I set about this week to explore making Wordpress post to my Twitter any time I update.

I found a basic, but functional, plugin called Twitpress, which does exactly what I wanted. Except…I’m also using the All In One SEO Pack, which rewrites page URLs into an SEO-friendly format. (Really, a must-have plugin.) Twitpress by default will tweet the stock version of a post URL:

http://RelevantText.com?p=24

instead of the format I want to show:

http://RelevantText.com/making-the-most-of-server-errors-20080111/

Now, I know that a)Twitter links are nofollowed, so this doesn’t really matter for the spiders, and b)Twitter also automatically turns long links into tinyurls, but it still bothered me (more on why in a minute). So, I set about to fix the plugin.

After reading through what the plugin code was doing, I surfed through the WP database tables a little bit, and discovered that I needed to change one line in Twitpress. Hooray!

In the twitpress.php code, replace line 85:

$proto = str_replace("[link]", get_option('home')."?p=".$postID, $proto);

with

$proto = str_replace("[link]", $post->guid, $proto);

‘guid’ is a field in the wp_posts table, if you care.

Bingo. I’m very pleased with myself.

So why, you may ask, do I care about how the links look in Twitter if they aren’t spiderable? Because I’ve also installed the Twitter App on Facebook, so any time I update Twitter, my Facebook status updates as well…which means the link is then being pushed out along the newsfeeds of all my contacts there. The link is still not spiderable, but it is potentially much more likely to get seen, followed, and possibly linked to. Through the tinyurl redirect, it now goes to the right version of the URL, and when people subsequently link to the post, I want them using the right one. This, I think, will help that along.

Jan 14 Update: After my initial excitement, I’ve discovered that this is still slightly buggy – notifications occasionally appear on twitter with the p= URL, and sometimes with no URL at all.  This seems to only happen when a post is first published, and not when later edited, but I’m not clear why, as the ‘guid’ field is populated with the first publish of a post. So, this is cool when it works, but I’m still looking at it. 

Share and Enjoy:
  • Digg
  • del.icio.us
  • Reddit
  • Sphinn
  • Technorati
  • Facebook

Leave a Reply

You must be logged in to post a comment.