• Home
  • New Entries
  • Popular Entries
  • Submit a Story
  • About

Use the Twitter and TinyURL APIs to Create a WordPress Plugin ...

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

AddThis Social Bookmark Button

Posted at 11:04:18 am | Permalink | Posted in WordPress Plugins  

Related Stuff

Google Buzz Button Wordpress Plugin Released

If you are looking for a Google Buzz button to add into your Wordpress site then we have released the first Wordpress plugin exclusively ...

Plug And Play Ecommerce With Wordpress Plugins

Since 2003 Wordpress has slowly been gaining popularity amongst the elite of the internet, the bloggers. It is one of those few things ...

Add Google Search to Your WordPress Blog

The 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 Theme

You 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 Wordpress

In my functions.php file, I have some code which implements series posting. This relies on the thematic ...

Top Stuff

Free Blogger templates Anime Themes

Wordpress Guestbook Generator Plugin

48 Unique Ways To Use WordPress

GeekLog

WordPress Single Post Templates

Zookoda



About Webloglines

Webloglines is a project offers a comprehensive collection of blogging services, articles, themes and plugins from around the world. Whether you're looking to promote your own blog or find blogs on various topics, this site is for you.


Search


Topics

  • Adsense (12)
  • Blogging Tips (73)
  • Blogs Slides (25)
  • Blogs Websites (22)
  • Digg (20)
  • How to Blog (129)
  • Search Engines (9)
  • SEO (135)
  • WordPress Plugins (269)
  • WordPress Security (75)
  • Wordpress Themes (99)
  • Wordpress Tips (162)

© 2006 www.webloglines.com. All Rights Reserved. Powered by IRange