Patch for YOURLS: WordPress to Twitter – Filter Hook to Control the URL that is Fed into Yourls

Yourls: WordPress to Twitter is a wonderful plugin which bridges yourls with wordpress. In addition to yourls statistics, I also wanted to track the clicks on the shortened link using Google Analytics. This requires addition of Google Analytics tags to the permalink URL which then gets shortened by yourls. Hence I modified the plugin code so that the URL that is fed into yourls can be modified the wordpress way – using a filter hook. Here’s the patch that I made.

diff -aurN yourls-wordpress-to-twitter/inc/core.php /home/pratikmsinha/humbug.in/wp-content/plugins/yourls-wordpress-to-twitter/inc/core.php
--- yourls-wordpress-to-twitter/inc/core.php  2010-03-09 12:37:14.000000000 -0800
+++ /home/pratikmsinha/humbug.in/wp-content/plugins/yourls-wordpress-to-twitter/inc/core.php  2010-07-01 18:23:39.000000000 -0700
@@ -61,7 +61,8 @@
  global $wp_ozh_yourls;
  
  $post_id = $post->ID;
- $url = get_permalink( $post_id );
+ //$url = get_permalink( $post_id );
+ $url = apply_filters('yourls_blog_post_url', get_permalink($post_id));
  
  if ( $post->post_type != 'post' && $post->post_type != 'page' )
    return;
@@ -71,7 +72,8 @@
    return;
  
  $title = get_the_title($post_id);
- $url = get_permalink ($post_id);
+ //$url = get_permalink ($post_id);
+ $url = apply_filters('yourls_blog_post_url', get_permalink($post_id));
  $short = wp_ozh_yourls_get_new_short_url( $url );
  
  // Tweet short URL ?
diff -aurN yourls-wordpress-to-twitter/plugin.php /home/pratikmsinha/humbug.in/wp-content/plugins/yourls-wordpress-to-twitter/plugin.php
--- yourls-wordpress-to-twitter/plugin.php  2010-03-09 12:37:14.000000000 -0800
+++ /home/pratikmsinha/humbug.in/wp-content/plugins/yourls-wordpress-to-twitter/plugin.php  2010-07-01 18:26:14.000000000 -0700
@@ -68,7 +68,8 @@
  if ( !$short && !is_preview() ) {
    // short URL never was not created before, let's get it now
    require_once(dirname(__FILE__).'/inc/core.php');
-   $short = wp_ozh_yourls_get_new_short_url( get_permalink($id), $id );
+   $url = apply_filters('yourls_blog_post_url', get_permalink($id));
+   $short = wp_ozh_yourls_get_new_short_url( $url, $id );
  }
  
  return $short;

To use the above patch, copy-paste the code into a file, lets say yourls.patch. Go into the yourls plugin directory (wp-content/plugins/yourls-wordpress-to-twitter/) and execute the following command.

patch -p1 < yourls.patch

Do ensure that you use the correct path to yourls.patch.