Created
January 5, 2012 19:19
-
-
Save joeyblake/1566752 to your computer and use it in GitHub Desktop.
get_bitly_link
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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