Use the Twitter and TinyURL APIs to Create a WordPress Plugin
Monday, April 20th, 2009
After the success of the release of our first WordPress plugin, Are You Sure?, we've decided to show you how to make a super simple WordPress plugin that uses the TinyURL and Twitter API. The plugin we will create will add a little link to the end of each individual post, this link will allow the users to send the title of the page they are reading, along with a tinyurl link to the page. Lets get coding!
The Basics
The first thing we need to do is create a new folder, I will call mine 'tiny_tweet'. Next, inside the directory, create a file named 'tiny_tweet.php'. Inside tiny_tweet.php we need to put some basic comments at the top to let WordPress properly recognize our plugin. So far our plugin looks like this:
<?php /* Plugin Name: Tiny Tweet Plugin URI: http://dev-tips.com Description: Allows users to tweet the current page with a tinyurl of the page in the tweet. Author: Drew Douglass Version: 1.0 Author URI: http://dev-tips.com */ ?>
Defining our function
Now we need to setup the function that will do all of the work for us. We want to make sure that the function does not already exists, so we will put this check into place.
if(!function_exists('dd_tiny_tweet_init')){
function dd_tiny_tweet_init($content){
}
}
Twitter and TinyURL
Lastly, we need to setup our variables and create the TinyURL of the current article so that we can put a link to the page when the user sends the page to their Twitter status. Lets look at the final code and then discuss it.
<?php
/*
Plugin Name: Tiny Tweet
Plugin URI: http://dev-tips.com
Description: Allows users to tweet the current page with a tinyurl of the page in the tweet.
Author: Drew Douglass
Version: 1.0
Author URI: http://dev-tips.com
*/
if(!function_exists('dd_tiny_tweet_init')){
function dd_tiny_tweet_init($content){
//Thanks to http://briancray.com for the one line $tiny_tweet_url solution.
$tiny_tweet_url = file_get_contents('http://tinyurl.com/api-create.php?url=' . urlencode('http://' . $_SERVER['HTTP_HOST'] . '/' . $_SERVER['REQUEST_URI']));
$tiny_tweet_title = get_the_title();
$tiny_tweet_title = substr($tiny_tweet_title, 0,100);
$tiny_tweet_title .='...';
$tiny_tweet_status_url = 'http://twitter.com/home?status=Currently reading "'.$tiny_tweet_title."\" ".$tiny_tweet_url;
if(is_single()){
$content .= '<div class=\'tiny_tweet\'>Enjoy this post? <a href=\''.$tiny_tweet_status_url.'\'>The give it a tweet!</a></div>';
}
return $content;
}
add_filter('the_content', 'dd_tiny_tweet_init');
}
?>
Most of the variables should explain themselves, but lets walk through some of the important steps in our function.
- First, we store the TinyURL url of the page inside a variable for later use. Please note that the technique to get the TinyURL was brought to my attention by my friend and Dev-Tips author Brian Cray in his post titled 'Generate TinyURLS with one line of php'. It is also important to note our server needs to support
file_get_contents. If you host doesn't, tell them to get with the times! - Next up we get the title of the current post and grab the first 100 characters of that title (to leave room for the link in the status).
- Moving on we store the status Twitter link in a variable with the appropriate message in the status query.
- Lastly, we check if the current page is a single article, if it is we append the twitter link to the bottom of the content with a custom div class (so it may be styled).
Now, when the user goes to read an individual article, they willl see 'Enjoy this post? Then give it a tweet!'. Upon clicking the link, they will be brought to twitter with their status already filled out. Now, they simply press the 'Update' button, and voila!
Demo Screenshots
Below is the default, unstyled link and text produced:

Below is what the user will see once they click the link. For those of you that will inevitably type the tinyurl in the screenshot below into your browser, that is a free hosing account I have setup for WordPress development and plugin testing

It isn't the most complex Twitter plugin, but it works just like it should and gets the job done. Feel free to use this plugin if you like. We will be making some final changes to it and will officially release it for download shortly in a new blog post.
If you enjoyed this article, you might consider subscribing to our rss feed to stay updated with all the latest tips and articles!









Very nice stuff. I will definately be using this in my next site.
Thanks!
Thanks Meshach, I’m glad you enjoyed it. Be sure to show it off when/if you use it on one of your sites, I’d love to see it
Great write up Drew! Very clear and easy to follow. Will probably add this plugin to my blog.
thanks!!
Thanks Mary, I’m thrilled to hear people would like to use this
Nice tut, thanks for sharing.
Should come in handy.
Your welcome, thanks for the comment!
Easy but very helpful. Thanks Drew =)
Anytime Gaya
Its a great explanation! Thx – although I don’t think I trust that bloke in the screenshot…
I thought he looked suspicious, wait….you again?!
*hides*
how would we add to page.php and single post?
The best information i have found exactly here. Keep going Thank you
Really cool code! Thanks for share
how can we modify for bitly?
Hi Jerome,
You’re in luck. See this post I wrote for ThemeForest for working with the bit.ly API instead: http://blog.themeforest.net/tutorials/creating-an-ajax-web-app-using-the-bitly-api/
You rock!
After reading this usefull article how to use twitter. I even see some sens in twittering. Thx a lot.