204 PART III • Using Plugins with WordPress
WRAPPING IT UP
There are a few little things left, although the AddAff plugin is working perfectly well at this
point. The most obvious problem would be that the affiliate links might not look all that great
on the theme you’re using, so some styling is needed. You’ll remember that the ul tag got the
#addaff ID, so it is easy enough to target. The li gets the addaff-item class as well, so
making this solitary link look good shouldn’t be too complicated.
However, it is just a solitary link. Perhaps you want more? You could easily add further fields
and loop them out instead of working with just the one. While you’re at it, you could define a
product image as well, making the links a bit more eye-catching.
Another thing the plugin needs is a simple uninstall.php file, so it cleans up after itself. I’ve
covered that already, so that’s a small thing to add.
The truly big thing that’s left, that really should be taken care of, is internationalization. The
plugin is prepared for it, but there is no defined textdomain (although the strings have
addaff, so they’re ready). Should this plugin be released, this is sorely needed as it is the
right thing to do.
ADDING WIDGET SUPPORT TO PLUGINS
Widgets make it easy to customize the contents of a blog or site. You can put them in the
widget area(s), dragging and dropping them from within the WordPress admin interface.
WordPress ships with a few widgets, such as one for outputting RSS, displaying the latest
posts, listing the Pages, listing the categories, and so on. These widgets may not be enough,
however, and when you create a plugin, you may want to give the user the chance to run it
from within a widget area. This is a lot more user friendly than having to put the plugin PHP
template tag in the theme’s template files, so it may be a good idea to widgetize your plugin if
it should be displayed in a widget area.
Thanks to the Widgets API (which you can read more about at the Codex page http://
codex.wordpress.org/Widgets_API), creating widgets for your plugins isn’t all that
complicated. You extend the built-in widget class, called WP_Widget, give it some instruc-
tions, and then register it so that it will show up, as shown in the following code:
class SmashingWidget extends WP_Widget {
function SmashingWidget() {
// The actual widget code goes here
}
function widget( $args, $instance ) {
// Output the widget content
}
function update( $new_instance, $old_instance ) {
// Process and save the widget options
}
function form( $instance ) {
// Output the options form in admin