How to Create Your Own Twitter Widget

twitter1

Even if there’s a lot of quality WordPress widgets available, sometimes none exactly fits what you need. In that case, you’ll have to create your very own widget. It may sound difficult at first, but it is not if you have a little programming experience.

In this tutorial, I’ll show you how to write a simple widget to allow your readers to share your posts on their Twitter accounts.

1 – Creating Files and the Directory

Let’s start by creating a directory on your hard drive. On this tutorial, I’ll call it Twit It. Under this directory, create a file named twitit.php.

Edit the twitit.php file and paste the following code, which is telling WordPress that this file is a widget, specify the plugin/widget name and version and also give the credits to the author:

1
2
3
4
5
6
7
8
9
<?php
/*
Plugin Name: Twit It
Plugin URI: http://www.wprecipes.com
Description: A simple "Twit It" widget for WordPress
Author: Jean-baptiste Jung
Version: 1
Author URI: http://www.catswhocode.com
*/

2 – Creating the Functions

Now, we have to create the main function, which will be called when your widget has been added to a widget-ready zone.

This function will simply echo a link to Twitter with the post URL as a parameter.

Append the code below to the twitit.php file:

1
2
3
4
5
function twitit() {
global $wp_query;
$thePostName = $wp_query->post->post_name;
echo '<div id="twitit"><a href="http://twitter.com/home?status=Currently reading '.$thePostName.' : '.get_permalink($post->ID).'" title="Click to send this page to Twitter!" target="_blank">Share on Twitter</a></div>';
}

Now, we have to deal with user defined parameters.

On the functions.php file from their theme, users can define some variables as such as $before_widget, $before_title, and so on. Theses variables are used to display a custom image, text or html tags within widgets. For example, the $before_title variable allow the users to specify something to appear before widget titles, to ensure a perfect theme integration.

This function will get all parameters from the functions.php file in order to make the widget perfectly integrated on the user’s theme.

1
2
3
4
5
6
7
8
function widget_twitit($args) {
extract($args);
echo $before_widget;
echo $before_title;
echo $after_title;
twitit();
echo $after_widget;
}

We’re almost done. We now have to create an initialization function, and hook the widget. This point is very important because it register the group of functions we just wrote as a widget.

1
2
3
4
5
6
function twitit_init() {
  register_sidebar_widget(__('Twit It'), 'widget_twitit');
}
 
add_action("plugins_loaded", "twitit_init");
?>

Save the file. You now have a fully functional “Send to Twitter” widget! You can install it on your blog like any other widget.

Styling the Widget

Now that you installed the widget, you saw how functional it is, but therefore, it is not very visually appealing. Happily, we added a #twitit css id on the container, which means that it will be easy to style it with CSS.

Simply paste the following styles to the style.css file from your theme. If you want, you can download and use the background image below.

twitit

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#twitit{
  background:#fff url(images/twitit.jpg) no-repeat top left;
  height:120px;
  position:relative;
  width:320px;
}
 
#twitit a{
  font-size:18px;
  font-weight:bold;
  position:absolute;
  top:30px;
  right:25px;
}

Complete Widget Code

Here’s the complete code I have used for the example widget:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?php
/*
Plugin Name: Twit It
Plugin URI: http://www.wprecipes.com
Description: A simple "Twit It" widget for WordPress
Author: Jean-baptiste Jung
Version: 1
Author URI: http://www.catswhocode.com
*/ 
 
function twitit() {
global $wp_query;
$thePostName = $wp_query->post->post_name;
echo '<div id="twitit"><a href="http://twitter.com/home?status=Currently reading '.$thePostName.' : '.get_permalink($post->ID).'" title="Click to send this page to Twitter!" target="_blank">Share on Twitter</a></div>';
}
 
function widget_twitit($args) {
  extract($args);
  echo $before_widget;
  echo $before_title;
  echo $after_title;
  twitit();
  echo $after_widget;
}
 
function twitit_init() {
register_sidebar_widget(__('Twit It'), 'widget_twitit');
}
 
add_action("plugins_loaded", "twitit_init");
?>

 

IMG1That’s all for now! As you saw, creating a WordPress widget is definitely easy. The widget we created together is quite basic, but I’m sure it is a solid basis to get started with widget development.

About the author: Jean-Baptiste Jung is a 27 year old blogger from the French-speaking part of Belgium. Jean blogs about WordPress on WpRecipes and about Web Design and Development on CatsWhoCode.com. You can follow him on Twitter.

Share

  1. Jack (8 comments)28 January 09

    Isn’t that blue bird from Iconfactory’s Twitterrific program?

  2. Nice :)

    Isn’t there already a plugin like that?? Or was that you..

  3. Great and very useful custom coded widget. Thanks for sharing.

  4. it easy because you show it,, to start from scratch,,
    emmm who knows..

  5. Seo cart (1 comments)29 January 09

    A friend of mine suggested I learn this and create my own widget – I never seem to like what’s available and would always change things ;) Thing is, I consider myself almost illiterate when it comes to computers ;) I can use them, but creating something from scratch scares me!
    Now I think this fear really was irrational as it seems like a fairly easy task. I know things will get harder as I go, but the start seems too good to pass it up :)
    Thanks for effectively killing my fear of programming !

    • Michael Martin (1319 comments)30 January 09

      Good luck with it all! It must seem daunting at first, but really, even when you make mistakes (Everyone does!), you can only learn from them. No harm done, so no need to be afraid! :D

  6. Alison (2 comments)29 January 09

    I tried writing my own plugin once, but I failed miserably at it. Maybe I’ll try this and mess around with it to more familize myself with the wordpress add_action thingies… *shrug*

  7. Alison (2 comments)29 January 09

    the click to edit feature is timed? is this a plugin? that’s so cool!

  8. Farid Hadi (15 comments)30 January 09

    Hey, thanks for this post. It gives a basic idea about creating WP widgets.

  9. Thanks for the lesson. I usually have to rely on others to create the code for me. This helps me figue out how to do it on my own.

  10. Do Follow (1 comments)3 February 09

    Story added…

    Your post was featured at Do Follow Community! Here is the link to vote it up and promote it: http://dofollow.net/Internet/...

  11. Khaled (2 comments)3 February 09

    If you wanted to learn a bit more about playing around with widgets would it be php you need to learn or is there more to it than that?

  12. Great Job your instructions are very easy to follow
    and precise again GREAT JOB

    Frank Gorka

  13. This is just perfect for my new blog! I have been meaning to find ways on how I can grab or create eye-candy twitter widgets like I find in most sites. So, this is exactly how it is done, thanks so much for sharing. The birdie btw, is really cute. :)

  14. Really well-done how to post. I’ve been wondering how to do this and the answer was presented very simply right here. Thank you!

  15. Snake (14 comments)9 February 09

    I will try it, it looks very good.

  16. I’ve been meaning to jump onto twitter or the forums and ask if anyone knows of a plugin which performs this exact function. I’ll be implementing this at my blog asap. Thanks for another useful post.

  17. awnputih (1 comments)12 February 09

    nice tips :)

  18. Just getting in to twitter, this is well cool,
    lots of new stuff coming out of twitter one to watch.

  19. Twitter rocks Thanks for sharing.

  20. How about a code for non widget users ? Thanks :)

  21. rajkumar (1 comments)21 February 09

    Really thanks for this code,.

  22. Michelle (3 comments)5 April 09

    I’d make this if it didn’t include the twitteriffic logo, which I don’t see an update to this post after several months of being notified of this pretty big boo-boo…

  23. Make Money (2 comments)1 May 09

    Cool. I would learn it and tell my twitter follows to put it on their blog (if they have) to follow me.

  24. You can make it as a plugin to make life easy for those technophobia folks.

  25. Glenn Bennett (1 comments)7 June 09

    Hey I created a tool that lets you do stuff like this a bit easier. It’s at widgetifyr.com. I do this all the time take a widget that works almost like I need then strip out the guts and create a new widget. Now with my new wordpress widget builder it’s super easy. Let me know that you think.

    Glenn

  26. Man, wish i would have found this post a couple months back. I was not satisfied at all with the premade widgets, not to mention the unreliable nature of it being hosted on a different server than my own. Nice tutorial, great post!
    .-= Josiah – Youtube Backgrounds´s last blog ..Call of Duty 4: Modern Warfare =-.

  27. r4i (2 comments)22 June 09

    It is very useful

  28. Dish Network (1 comments)12 July 09

    App Tool…

  29. ote tatsuya (1 comments)24 July 09

    is this widget can be used on blogger?or may you have another widget like this, that provide for blogger user?
    .-= ote tatsuya´s last blog ..Links for 2009-07-22 [Digg] =-.

  30. Woww…nice tutorial, may be it can be implemented on other CMS…hemmm…let me try it…thanks :D

  31. r4i lover (1 comments)16 August 09

    Ooh perfect, I have added my own twitter widget to my website :D .

  32. nice share buddy its really easy I try this its working well here. Now I have my own twitter widget. Thanks for sharing…..

  33. Nits (6 comments)2 September 09

    Every one using the twitter ,great help for all blogger thanks.

  34. Packam (1 comments)15 September 09

    Thanks a lot for this tutorial.

  35. wow, that was so helpful and it looks so easy now compared to when i was starting. thanks

    freebies

  36. yahoo (2 comments)25 September 09

    Hey I created a tool that lets you do stuff like this a bit easier. It’s at widgetifyr.com. I do this all the time take a widget that works almost like I need then strip out the guts and create a new widget. Now with my new wordpress widget builder it’s super easy. Let me know that you think.

  37. yahoo (2 comments)25 September 09

    Hey I created a tool that lets you do stuff like this a bit easier. It’s at widgetifyr.com. I do this all the time take a widget that works almost like I need then strip out the guts any. Let me know that you think.

  38. yrccojbmnmvz

  39. Seems like a complicated thing to get right, but this is by far the best tutorial I have read on the topic to date. Keep the great articles following friend.:D

  40. Hi there,
    Nice information really helpful share i also try it and amazingly its start work I am very happy to create my own twitter widget thanks dude for sharing.

  41. jacob (1 comments)27 October 09

    is twitter a big site that rolled on hype, celebrity power and media’s repetition. I hope Ashton Kutcher made some money for promoting twitter. Now everybody is tweeting. :)

  42. just what i was looking for, thanks for sharing the codes.

    women clothes

  43. thank you for this!

  44. I am currently working on a widget based on this post. Thanks!

  45. Freebies (2 comments)29 November 09

    Thanks this is great!!!

  46. rosario (12 comments)1 December 09

    Really well done thanks for sharing :)

  47. What else can I say, but thank you. Have yu tried submitting this to the wordpress plugin directory.

  48. Hi there,
    Nice information really helpful share i also try it and amazingly its start work I am very happy to create my own twitter widget thanks dude for sharing.

  49. very useful….thanks!

  50. Great tutorial. Thanks for sharing. I may now have the knowledge to give it a go!

  51. Awsome tutorial, Im still a bit novice for this excersise but im going to work at it for the next couple days.
    Thanks alot for the post!

  52. How do I make a link, so when users click on the link, it will redirect them to twitter and ready to tweet a redefine tweet.

  53. Blogington (1 comments)7 March 10

    Excellent instructions and very easy to follow. I’m new at this more techie stuff but I think I will give it a try. Thanks for sharing it with us.

  54. it cushy because you exhibit it,, to move from scratch,,

Leave a Comment

Your reply will be added to the comment above (Below any other replies to this comment) -

(We DoFollow)

Not sure how to get an image with your comment?