Making A Wordpress 2.5 Sidebar Widget Plugin

by justin on April 22, 2008

Wordpress 2.5 And Widgets

A Wordpress Widget is a dynamic element that can be placed in the sidebars (or anywhere that is declared a sidebar) in your Wordpress theme. For example on this blog there is a right sidebar with these widgets: Search It, Recent Entries, Categories, Related Sites, Recent Visitors (by Feedjit), Recent Readers (by MyBlogLog). A widget can be any small modular piece of html (usually a list but not always) that can be stuck into a sidebar.

Widgets Make Your Plugin More Flexible

If you write your plugin as a widget your users will easily be able to move your plugin to which ever sidebar they want and arrange it with their other widgets how they want to. And they can do all of this without touching their theme’s code. This is great for users who don’t know how to modify a Wordpress theme template.

Sidebars Are Not Just Sidebars

Though they are called sidebars, they can actually be any block element of the page. A sidebar could be a heading, a footer, a section within a particular page, anything you want. It’s easy to declare a sidebar. Usually sidebars are declared within a theme’s functions.php file.

Declaring Your Sidebars – From Within A Theme

Update: There’s a great tutorial just on making your theme widget ready over here.
For example in a theme I’m working on I have 3 sidebars, normal left and right sidebars, and a top navigation bar that I’m also using as a dynamic sidebar for widgets. In my functions.php file I have:

register_sidebar(array(
    	'name'=>'top_sidebar',
        'before_widget' => '<li id="%1$s" class="widget %2$s">',
        'after_widget' => '</li>',
        'before_title' => '<h2 class="widgettitle">',
        'after_title' => '</h2>',
    ));
    register_sidebar(array(
    	'name'=>'left_sidebar',
        'before_widget' => '<li id="%1$s" class="widget %2$s">',
        'after_widget' => '</li>',
        'before_title' => '<h2 class="widgettitle">',
        'after_title' => '</h2>',
    ));
    register_sidebar(array(
    	'name'=>'right_sidebar',
        'before_widget' => '<li id="%1$s" class="widget %2$s">',
        'after_widget' => '</li>',
        'before_title' => '<h2 class="widgettitle">',
        'after_title' => '</h2>',
    ));

You can see some basic documentation on register_sidebar here. The name=>top_sidebar’ tells Wordpress what I’m calling my sidebar. This name will be displayed in the Admin interface under Design->Widgets->Current Widgets in the drop down list. You also use this name to place all the sidebar widgets within your them template. Users can choose to add widgets to any of the declared sidebars. The before_widget, after_widget, before_title, and after_title specify some html to wrap the widget in. This is handy if you need to style the widgets in a special way for your theme.

Using Dynamic Sidebars Within Your Theme

To use your sidebars within a theme you just call the dynamic_sidebar function. Here’s how I do it in my theme:

<?php if (function_exists('dynamic_sidebar')) {
dynamic_sidebar('right_sidebar');
} ?>

This tells Wordpress to call the display functions for all the widgets the user has added to the ‘right_sidebar’ sidebar.

A Hook To Hang Your Widget From

You’ll need to tell Wordpress about your plugin’s widget. To do this you declare a “plugins_loaded” action hook like so:

    function mycoolplugin_loaded()
    {
        $widget_ops = array('classname' => 'mycoolplugin_widget', 'description' => "A very cool widget for your sidebar." );
        wp_register_sidebar_widget('mycoolplugin_widget', 'CoolPlugin', 'mycoolplugin_widget', $widget_ops);
    {
    add_action('plugins_loaded','mycoolplugin_loaded');

In your widget you’ll want to replace mycoolplugin with the unique name you’ve chosen for your plugin. And you’ll want to replace “A very cool widget for your sidebar.” with the real description for your widget.

A Note For Uber-Geeks

So if you read the documentation here they will tell you to use register_sidebar_widget instead of wp_register_sidebar_widget. I found register_sidebar_widget really frustrating because I couldn’t get it to display the description of my widget (above “A very cool widget for your sidebar.”). The internal widgets like Pages and Categories use the wp_register_sidebar_widget to declare themselves. If your interested you can find the these functions in wp-includes/widgets.php.

Your Widget Display Code

In the code above we specified mycoolplugin_widget as the callback function for Wordpress to use when it wants to display your widget. So we’ll need to declare this function and use it to generate our widget’s HTML code.


function mycoolplugin_widget($args)
{
	extract($args); // extracts before_widget,before_title,after_title,after_widget
	echo $before_widget . $before_title . 'CoolPlugin' . $after_title . "<ul>";
	for ($i=0; $i<10; $i++)
	{
		echo "<li>List Item $i</li>";
	}
	echo "</ul>" . $after_widget;
}

My widget simply displays a header of “CoolPlugin” and then a list of ten items. Of course you’ll want to make your widget do something much cooler, but I’ll leave that to you!

Further Reading & Resources

Most of these resources a little out of date for Wordpress 2.5. Most of the screen shots show the admin widget pannel for 2.3 instead of 2.5.

{ 13 comments }

Pick Up A Great Website For Cheap

by justin on September 30, 2007

Shark

We’ve all seen those websites that show up in the SERPs that are just there because the domain has been around for years and has managed to pick up a few links along the way. They’re listed in dmoz, PR 4 or 5, but the content is really old. What if there was a way to dig through the list of sites in your niche and find the few that been abandoned by their owners? Don’t you think a some of those owners would be willing to sell you their websites far below their true value? They are! You just need to find them.

Old, Authoritative, Ranking Websites Are Waiting For You

I built a tool that will help you find sites you can buy that are old and authoritative in your niche. There are websites in almost every niche that have built up authority over years but have been abandoned by their owners. These absentee owners are often happy to unload their old websites on you for very little. The trick is finding these hidden gems amongst all the active websites. You can spend hours going through directories and SERPs looking for websites that are abandoned or you can use the tool I built to find them quickly and automatically.

Sitefinder301 – Find A Killer Deal On An Old Website.

Sitefinder301 uses the dmoz Open Directory and the Internet Archive to find sites that are relevant to your niche that haven’t been updated in a long time. If you’re lucky you can find a great site with an owner that is willing to sell it for very little.

How It Works

You supply a dmoz category URL and Sitefinder301 examines all the sites that are in it and finds the ones that haven’t been updated in a long time. It gives you PageRank and Whois information on the sites so you can quickly determine if it’s worth buying and who to contact. It’s a quick and easy way to come up with a short list of acquisition targets out of hundreds of possible websites.

Enough Already Where’s The Goods?

Ok here it is, the product of late night php hacking:

Find Killer Deals On Old Websites: Sitefinder301

Benefiting From Your Acquisition

Here’s a couple ideas for what to do with your new site:

  • Put up new content and rank quickly for valuable terms. Build upon the old sites authority and create something of real value.
  • Put up a page that promotes your product or service and funnels visitors to your current website.
  • Redirect the old site to your current site and benefit from it’s authority and traffic.

Share And Share Alike

Sitefinder301 is open source. You can install it on your SEO tools website or hack it for your own purposes. Download the source here: /makebeta/sitefinder301/sitefinder301code.zip
All I ask is you pass some link friendship back my direction :) .

{ 17 comments }

Floating Point Comparisons In PHP and Javascript

September 14, 2007

A simple function to ease your floating point comparison headaches. Implemented in PHP and Javascript. Compare floating point numbers safely like this: moneycomp($income,’>',$debt) return ‘Happy’; Escape the plague of the .000000001!

Read the full article →

Scraping Links With PHP

August 11, 2007
Abstract Network

In this tutorial I will show you how to build a PHP script that will scrape links from a web page. I also discuss the legal issues involved with scraping website content.

Read the full article →

Facebook PHP Tutorial

July 31, 2007
F8 Logo

I will cover the basics of using the Facebook PHP Client Library and how to get your application started, including:

  1. Installing The Facebook Developer Application
  2. Downloading The Facebook PHP Client Library
  3. Creating Your Application
  4. Hello Facebook! Example
  5. FBML – Facebook Markup Language
  6. Using the Facebook API
Read the full article →

The Spy Is Dead – Spyjax Offline

June 15, 2007

I will no longer be hosting Spyjax.
It’s been fun and very interesting, but it’s time to call it quits. Spyjax is just a side project but it’s eating up server power so I’ve decided to turn it off. I kicked around the idea of turning into a real service / business but I’m just not [...]

Read the full article →

Spyjax – Your browser history is not private!

May 23, 2007

If you’re like most web users, you assume that your browser history is private. For example if you visit an online store, you assume they can’t see if you’ve been looking at their competitor. Just a few weeks ago I assumed this was the case. Guess what? Your browser history is not private!

Read the full article →

WordPress Customization

May 9, 2007

A presentation on customizing WordPress. It gives an introduction to WordPress, and covers the basics of customizing themes.

Read the full article →