Hack Sociable to work with YOURLS – Quick and Dirty

2009 September 21
by Paul O'Flaherty
Code

Code

Since we’ve created Scrw.us and started using our own custom short URLs for our domains we’ve run into a few issues with other WordPress plugins not working quite the way we would like them to.

The biggest issue we’ve had is with the awesome sociable plugin by Joost De Valk (now taken over by BlogPlay) which adds social bookmarking buttons to all your posts. It’s an amazing plugin and even allows you to use it with URL shortening service awe.sm, but of course we wanted to use our own service which runs on Yourls.

When I first made the modifications to Sociable I didn’t realized that some services, such as StumbleUpon, would throw a fit when they got passed the scrw.us URL. Essentially they were having problems with the 301 redirect, so I went back and did it a second time.

I didn’t want to strip out the awe.sm shortening functionality (in case I ever needed to fall back on it) from the script and wanted to be able to selectively decide which services to send a short URL to and which to send the original URL to.

My solutions isn’t exactly elegant and there are certainly other ways to approach it, but it works.

First working with version 3.4.4 of Sociable, open the file “sociable.php” in your favorite editor (I’m working in Dreamweaver) and find line 605.

Line 605 should look like this:

$permalink = urlencode(get_permalink($post->ID));

Replace it with the following:

$permalink = urlencode(get_permalink($post->ID));
$shortlink = urlencode(wp_ozh_yourls_raw_url());

Next find line 679 which reads:

$url = str_replace(‘PERMALINK’, $permalink, $url);

Replace line 679 with the following:

$url = str_replace(‘PERMALINK’, $permalink, $url);
$url = str_replace(‘SHORTLINK’, $shortlink, $url);

All that we’ve done here is create new variable called “$shortlink” which grabs the short URL via a call to the YOURLS API (more info on the API here) and allows us to place the short URL into the array which is used to construct the URL used in the bookmarks.

The array for constructing the URLS is between lines 62 and 552.  The section for posting to Twitter looks like this:

Twitter’ => Array(
‘favicon’ => ‘twitter.png’,
‘awesm_channel’ => ‘twitter’,
‘url’ => ‘http://twitter.com/home?status=TITLE%20-%20PERMALINK’,

As it is above, the twitter bookmark that appears on the bottom of your post will send the full URL of your post to twitter along with the title. In order to make it work with your short URL just swap out “PERMALINK” for “SHORTLINK”:

‘Twitter’ => Array(
‘favicon’ => ‘twitter.png’,
‘awesm_channel’ => ‘twitter’,
‘url’ => ‘http://twitter.com/home?status=TITLE%20-%20SHORTLINK’,

Now all you have to do is decide which services you want to send short URLs to and which you want to get the full URL and replace as necessary inside the array.

As a note, we’re using the short URL for Facebook, Twitter, Email and Friendfeed and using the full URL for Digg and StumbleUpon. You may need to test which ones work for you, depending on the services you use, as they don’t all accept the short URL.

3 Responses leave one →
  1. September 21, 2009

    Thanking you kindly, and yes, I'll get to nagging him :)

  2. June 15, 2010

    Thanks for the tip! Exactly what I was looking for.

    I sure wish that they would add this as an option in the Settings in Sociable.

Trackbacks and Pingbacks

  1. Paul OFlaherty

Leave a Reply:

Note: You can use basic XHTML in your comments. Your email address will never be published.

Never miss an update. Subscribe by RSS