How to — Programming a WordPress Sidebar Widget Plugin

A friend of mine recently asked me to take a look at some code that he’d written and see if I could get it to work. I’m not going to tell you what was wrong with his code, but I will tell you that in the process of troubleshooting the code I got the idea for this post. Let’s look at how we can make a full, working WordPress sidebar widget.

There are at least two ways to make a sidebar widget in WordPress. The first is to create a file in the “wp-content/plugins” directory and activate the widget as a regular plug-in, and second is to write the code in the “functions.php” file included with your WordPress theme. I have used both and they both work just fine. I will use the first method here just because I regard sidebar widgets to be WordPress plugins.

I encourage you to work through this so that at the end you will have your fully functional WordPress sidebar widget. I’m going to call our widget “Your Lucky Number.” That is, the widget will pick a random number from a certain interval and display it to the reader.

So, look up the “wp-content/plugins” directory in your WorPress installation and in it create a new “lucky_number_widget” directory, which will contain a “lucky_number_widget.php” file, as you see in the photo below.

WordPress tutorial -- file path

The file that you just created will contain all the code needed for our little widget. At the top of this file put the following code:


<?php
/*
Plugin Name: Lucky Number Sidebar Widget
Plugin URI: http://tektok.romagazin.net
Description: This widget picks a lucky number
Author: Your name here
Version: 1.0
Author URI: http://tektok.romagazin.net
*/
?>

In PHP, that’s called a comment block. That is, a block of text, between “/*” and “*/”, that has no meaning in PHP; it is only meaningful to humans. However, that kind of comment block, placed at the top of a PHP file, which resides in the “plugins” directory, has a very important significance to the WordPress platform. It tells WordPress that this file contains a plugin for the platform. As a consequence, WordPress will give you an opportunity to activate the plugin in the administration panel. If you don’t put this comment block as it appears in the above code, you will not be able to activate the plugin, therefore you won’t be able to use it. After you’ve saved the file, go to your admin control panel and click on “plugins;” you should see something similar to this picture:

WordPress Plugins Table

OK, now it’s time we created the code that drives our widget. Put the following code right below the comment block:


function lucky_number_widget($args){
extract($args);

echo $before_widget;
echo $before_title . 'Your lucky number is:' . $after_title;

echo '<p style="font-size: 5em">' . rand(1, 20) . '</p>';
echo $after_widget;
}

In the above code: line 01 defines the function that will be called every time our WordPress widget is displayed in our blog. Line 02 is a little tricky for those who are new to PHP. Please read about the PHP extract function if you need to learn about it. To keep this tutorial short, I will only say that when the widget has to be displayed in the sidebar, WordPress calls our function with an associative array as parameter ($args). This array has the keys before_widget, after_widget, before_title and after_title and their corresponding values are string representations of HTML tags. extract transforms the key-value pairs in actual PHP variables, just as we would define $var = ’some string’;. The rest of the code is simple: it just echoes those values in the order they are meant to be echoed. Line 07 displays a random number between 1 and 20 inclusive within a HTML paragraph. That’s your lucky number!

Activate the plugin

Having coded all the functionality of our widget, we now have to write a function that WordPress will call when it loads the active plugins. This function will register our sidebar widget with the WordPress platform, so that it appears in the “Available Widgets” list under “Design->Widgets” menu.


function init_lucky_number_widget(){
//this function is called when WordPress loads the plugins
register_sidebar_widget('Lucky Number Widget', 'lucky_number_widget');
}

Just drop the above code in our file right below the lucky_number_widget() function. Read more about register_sidebar_widget().

One more line of code and then you’ll be able to see results. Under the function init_lucky_number_widget() type the following line of code:


add_action('plugins_loaded', 'init_lucky_number_widget');

Now save the file and go to your WordPress control panel. Click on “plugins” and then click “Activate” for “Lucky Number Sidebar Widget.” Click on “Design” and then on “Widgets” under it. You should see something like this:

After activating the widget

Click the “Add” link next to its name and look to the right in the “Current Widgets” list. After you arrange it in the position you want, you should see something like this:

Current Widgets

Click “Save” under the list. If you have a widgetized WordPress theme, you should see your widget appear on the sidebar of your blog. It should appear similar to how it appears on the sidebar of this blog; that’s your lucky number. If you don’t know what to do with it, look at my lottery widget and maybe it’ll help you hit the jackpot :-).

Congratulations! You’ve just created your first working WordPress sidebar widget. Next time, we will look at how to enhance it. We’ll write the code that allows us to modify the range of the lucky number or maybe something else. Until then, drop me a comment; tell me how it went. If you encountered problems, by all means, throw them my way. We’ll get it working!

  • E-mail this story to a friend!
  • Print this article!
  • Technorati
  • Digg
  • Google
  • Facebook
  • del.icio.us
  • Blogsvine

Should I Buy the New iPhone?

A year ago I was able to resist the initial launch of the iPhone because I was in the middle of a 2-year contract with T-Mobile. This time, however, resisting it proves to be a little harder; the new iPhone 3G is a lot less expensive than its predecessor and promises 3G capability, which any intense user would like to have. Moreover, my contract with T-Mobile has expired and I’am quietly paying by the month until I decide to make a move.

But going for the iPhone 3G isn’t such a no-brainer as some might think. Not for me, anyway. I’ll tell you why. First because when I signed the 2-year contract with T-Mobile I got the T-Mobile Wing for free through letstalk.com. They even shipped it to me free of charge. All I had to do was to fill out an online form and a couple of days later I found it in my mail box. That’s a savings of at least $200 plus the convenience of having it come to me as opposed to going to the store and standing in line for it.

Second, I don’t see that much of a difference in the feature pack. I read my email almost exclusively on my phone, I browse the Internet on my phone whenever I can’t use the laptop. (I still prefer a big screen for browsing the Internet. The websites that I use regularly don’t display quite the same on the cellphone screen.) I can read/write Word, Excel and PowerPoint documents (a feature that the iPhone doesn’t have) and I can do anything that you are expected to be able to do on a cellphone. Oh yea, I also listen to music on my phone ;). So, why should I get the iPhone?

And then there is this crazy idea that the new iPhone works only on the AT&T network. Even if you unlock it, there are so many people that report that as soon as you upgrade the firmware you’ll have to unlock it again and re-install all the applications that are not “kosher” to Apple. Do I really need this aggravation? I’ve had my T-Mobile Wing unlocked in Romania when I traveled there last year and now I can use the phone with any SIM card with all functionality intact. I remember I paid a guy $10 to unlock my phone; however, someone told me that T-Mobile customer service will give you the unlock code for free (I didn’t try it, though) — supposedly all you have to do is call them. Again, why get stuck with the iPhone on AT&T?

There! I’ve worked up enough courage to say something against the Apple hype machine. I know I’m not alone in thinking this way. One thing: I welcome comments especially when the authors disagree with me, but please stop short of using objectionable language. There is nothing uglier that a person who resorts to ugly language. Now, let’s disagree.

  • E-mail this story to a friend!
  • Print this article!
  • Technorati
  • Digg
  • Google
  • Facebook
  • del.icio.us
  • Blogsvine