Advertisement
Guest User

Untitled

a guest
Jan 7th, 2011
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.80 KB | None | 0 0
  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);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement