This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Jan 7th, 2011  |  syntax: PHP  |  size: 0.80 KB  |  views: 181  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. function yoast_bitly_shortlink($url, $id, $context, $allow_slugs) {
  2.         if ( ( is_singular() && !is_preview() ) || $context == 'post' ) {
  3.                 $short = get_post_meta($id, '_yoast_bitlylink', true);
  4.                 if ( !$short || $short == '' ) {
  5.                         if ( !defined('BITLY_USERNAME') || !defined('BITLY_APIKEY') ) {
  6.                                 $short = 'http://yoast.com/wordpress/bitly-shortlinks/configure-bitly/';
  7.                         } else {
  8.                                 $url = get_permalink( $id );
  9.                                 $resp = wp_remote_get('http://api.bit.ly/v3/shorten?format=txt&longUrl='.$url.'&login='.BITLY_USERNAME.'&apiKey='.BITLY_APIKEY);
  10.                                 if (is_array($resp))
  11.                                         if ( $resp['response']['code'] == 200 ) {
  12.                                                 $short = $resp['body'];
  13.                                                 update_post_meta( $id, '_yoast_bitlylink', $short);
  14.                                         }
  15.                         }
  16.                 }
  17.                 return $short;
  18.         }
  19.         return false;
  20. }
  21. add_filter('pre_get_shortlink','yoast_bitly_shortlink',99,4);
clone this paste RAW Paste Data