After the success of the release of our first WordPress plugin, Are You Sure?, we have 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 iny_tweet. Next, inside the directory, create a file named iny_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:
view sourceprint?
01.<?php
02./*
03.Plugin Name: Tiny Tweet
04.Plugin URI: http://dev-tips.com
05.Description: Allows users to tweet the current page with a tinyurl of the page in the tweet.
06.Author: Drew Douglass
07.Version: 1.0
08.Author URI: http://dev-tips.com
09.*/
10.?>
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.
view sourceprint?
1.if(!function_exists( wouldd_tiny_tweet_init)){
2.
3.function dd_tiny_tweet_init($content){
4.
5. }
6.}
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.
view sourceprint?
01.<?php
02./*
03.Plugin Name: Tiny Tweet
04.Plugin URI: http://dev-tips.com
05.Description: Allows users to tweet the current page with a tinyurl of the page in the tweet.
06.Author: Drew Douglass
07.Version: 1.0
08.Author URI: http://dev-tips.com
09.*/
10.if(!function_exists( wouldd_tiny_tweet_init)){
11.
12.function dd_tiny_tweet_init($content){
13. //Thanks to http://briancray.com for the one line $tiny_tweet_url solution.
14. $tiny_tweet_url = file_get_contents(http://tinyurl.com/api-create.php?url= . urlencode(http:// . $_SERVER[HTTP_HOST] . / . $_SERVER[REQUEST_URI]));
15. $tiny_tweet_title = get_the_title();
16. $tiny_tweet_title = substr($tiny_tweet_title, 0,100);
17. $tiny_tweet_title .=...;
18. $tiny_tweet_status_url = http://twitter.com/home?status=Currently reading ".$tiny_tweet_title."" ".$tiny_tweet_url;
19. if(is_single()){
20. $content .= <div class= iny_tweet>Enjoy this post? <a href=.$tiny_tweet_status_url.>The give it a tweet!</a></div>;
21. }
22. return $content;
23. }
24. add_filter( he_content, wouldd_tiny_tweet_init);
25.}
26.?>
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 , 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 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!
Original Source:http://dev-tips.com/featured/use-the-twitter-and-tinyurl-apis-to-create-a-wordpress-plugin
Related Stuff
Add Google Search to Your WordPress BlogThe native WordPress search does not return very relevant results, thus it makes a lot of sense to add Google Search into your WordPress ...
Add More Sidebars to Your WordPress ThemeYou can add more than one sidebar section to your WordPress site. For example, with the stc-intermountain.org site, I added a whole bunch ...
Series Posting in WordpressIn my functions.php file, I have some code which implements series posting. This relies on the thematic ...
10 Alternative Uses for WordPressWordPress is the best and most popular blogging platform around. It’s free, easy to use, endlessly customizable and supported by ...
Using Google Code libraries with WordPressBy default, WordPress loads the version of jQuery that comes with the package. But there is an alternative way of doing it and that’s ...
