
Bit.ly is currently the most popular URL shorting service out there and for a good reason too. With bit.ly you can track your shortened URLs and much more. In this article I’m going to show you how to use bit.ly’s api create bit.ly urls automatically for WordPress posts.
In order to make use of Bit.ly’s API, you’ll need to:
- Signup at Bit.ly
- Get your API Key
Now that you have a login and API key, open your WordPress theme’s functions.php (just create one if you don’t have one) and paste the following code at the top of the document:
05 |
$url = get_permalink(); //generates wordpress' permalink |
06 |
$login = 'imjp'; //your bit.ly login |
07 |
$apikey = 'R_11882237eac772b5d6126e895a06c43f'; //bit.ly apikey |
08 |
$format = 'json'; //choose between json or xml |
15 |
//could also use cURL here |
16 |
$response = file_get_contents($bitly); |
18 |
//parse depending on desired format |
19 |
if(strtolower($format) == 'json') |
21 |
$json = @json_decode($response,true); |
22 |
echo $json['results'][$url]['shortUrl']; |
26 |
$xml = simplexml_load_string($response); |
The code above is pretty much self explanatory.
Don’t forget to change the login and apikey strings to match yours.
I’ve modified David Walsh’s “Create Bit.ly Short URLs Using PHP” in order to generate bit.ly urls automatically with WordPress. Now you can display your bit.ly url anywhere inside the loop or post .
The result of <?php bitly(); ?> in this post is http://bit.ly/4UbQ66.
I hope you guys found this article useful
!
You Might Like This PostsHow to change blog title be SEF
Social Bookmarking For WordPress Blog
Easy to Upgrade Wordpress Engine
Learn about WordPress SEO
How To Integrate Wordpress with Drupal CMS
This entry was posted
on Thursday, January 28th, 2010 at 1:25 am and is filed under SEO, tips, wordpress.
You can follow any responses to this entry through the RSS 2.0 feed.
You can skip to the end and leave a response. Pinging is currently not allowed.