
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.

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"); ?> |
That’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.
Custom Search
Jack (8 comments)28 January 09
Isn’t that blue bird from Iconfactory’s Twitterrific program?
6omma (1 comments)27 February 09
mmm yeah! thay shouldn’t be there (not mention about Twitterrific… and you erased the “Twitterrific” text) :S … http://twitter.com/Twitterrific
Brad Blogging.com - Personal Blog Tips And Blog Help (8 comments)28 January 09
Nice
Isn’t there already a plugin like that?? Or was that you..
Web Design Beach (28 comments)28 January 09
Great and very useful custom coded widget. Thanks for sharing.
izzat aziz (3 comments)29 January 09
it easy because you show it,, to start from scratch,,
emmm who knows..
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 (1285 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!
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*
Alison (2 comments)29 January 09
the click to edit feature is timed? is this a plugin? that’s so cool!
Michael Martin (1285 comments)30 January 09
Yep, it’s called AJAX Edit Comments. You can check it out here: http://wordpress.org/extend/pl.....-comments/
Farid Hadi (15 comments)30 January 09
Hey, thanks for this post. It gives a basic idea about creating WP widgets.
Dan London (1 comments)1 February 09
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.
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/...
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?
Frank Gorka (1 comments)4 February 09
Great Job your instructions are very easy to follow
and precise again GREAT JOB
Frank Gorka
Make Money (2 comments)4 February 09
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.
Webdesigner Depot (1 comments)6 February 09
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!
Snake (14 comments)9 February 09
I will try it, it looks very good.
Tracey Grady (14 comments)11 February 09
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.
awnputih (1 comments)12 February 09
nice tips
uninstall tool (1 comments)14 February 09
Just getting in to twitter, this is well cool,
lots of new stuff coming out of twitter one to watch.
unlock iphone (1 comments)14 February 09
Twitter rocks Thanks for sharing.
Freeman59 (1 comments)17 February 09
How about a code for non widget users ? Thanks
rajkumar (1 comments)21 February 09
Really thanks for this code,.
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…
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.
Michael Martin (1285 comments)5 May 09
Ah, that’s a clever idea! Very good use for it. Let us know how it works out!
Howto MakeMoney (1 comments)4 May 09
You can make it as a plugin to make life easy for those technophobia folks.
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
Josiah - Youtube Backgrounds (1 comments)15 June 09
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 =-.
r4i (2 comments)22 June 09
It is very useful
Dish Network (1 comments)12 July 09
App Tool…
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] =-.
Andre Lukmana (1 comments)7 August 09
Woww…nice tutorial, may be it can be implemented on other CMS…hemmm…let me try it…thanks
r4i lover (1 comments)16 August 09
Ooh perfect, I have added my own twitter widget to my website
.
Online Printing (9 comments)26 August 09
nice share buddy its really easy I try this its working well here. Now I have my own twitter widget. Thanks for sharing…..
Nits (6 comments)2 September 09
Every one using the twitter ,great help for all blogger thanks.
Packam (1 comments)15 September 09
Thanks a lot for this tutorial.
freebies (2 comments)22 September 09
wow, that was so helpful and it looks so easy now compared to when i was starting. thanks
freebies
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.
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.
nbjedophpzgq (1 comments)5 October 09
yrccojbmnmvz
How to Make Money 2K (1 comments)15 October 09
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
Canada Website Design (4 comments)15 October 09
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.
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.
women clothings (1 comments)27 October 09
just what i was looking for, thanks for sharing the codes.
women clothes
norfolk web design (1 comments)18 November 09
thank you for this!
Top 3 Acai Berry (1 comments)27 November 09
I am currently working on a widget based on this post. Thanks!
Freebies (2 comments)29 November 09
Thanks this is great!!!
rosario (10 comments)1 December 09
Really well done thanks for sharing
Eau parfum (2 comments)7 December 09
What else can I say, but thank you. Have yu tried submitting this to the wordpress plugin directory.
Springer kits (1 comments)26 January 10
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.
newport news italian restaurant (1 comments)27 January 10
very useful….thanks!
London Wedding Photographers (1 comments)5 February 10
Great tutorial. Thanks for sharing. I may now have the knowledge to give it a go!
Dating forum opinions (1 comments)5 February 10
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!