Skip to content

Instantly share code, notes, and snippets.

@joeyblake
Created January 5, 2012 19:19
Show Gist options
  • Save joeyblake/1566752 to your computer and use it in GitHub Desktop.
Save joeyblake/1566752 to your computer and use it in GitHub Desktop.
get_bitly_link
function get_bitly_link($post_id){
$post = get_page($post_id);
if($post->post_status == "published" || 1 == 1){
if(false === ($short_link = get_post_meta($post_id,'bitly_link',true))){
$http = _wp_http_get_object();
$args['login'] = self::BITLY_USERNAME;
$args['apikey'] = self::BITLY_APIKEY;
$args['longUrl'] = get_permalink($post_id);
if(is_wp_error($response = $http->request('https://api-ssl.bitly.com/v3/shorten?'.http_build_query($args),array('method'=>'GET')))){
return $response;
}else{
$response = json_decode($response['body']);
add_post_meta($post_id,'bitly_link',$response->data->url);
$short_link = $response->data->url;
};
}
}else{
return false;
}
return $short_link;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment