Addactioninit wickedremoveindexinsert

And that's it! If you refresh the widget admin page now, the index-insert box will have vanished.

Adding Custom Widgets

There are tons of plugins and widgets in the WordPress plugin directory; however, at some point you'll need to come up with your own custom widget that solves a particular problem. It's a really great selling point to have themes that come with built-in widgets, especially when they take advantage of a particular feature only available to your theme.

Traditionally, there were two ways to add your own custom widgets. The first was to simply add a function to your functions.php file, which is what we've been doing so far for the custom functionality of our theme. This will become quite cumbersome quickly, though with good commenting and file separation it can stay manageable. The second way of going about it is to create a WordPress plugin for each new widget. This tends to be problematic as well, because the plugin will exist separately from your theme, thereby adding a few extra administrative steps to the installation process. You want your theme to be as easy as possible to use, so this is probably best avoided.

Luckily, Thematic comes with a rather innovative built-in widget solution that makes widget creation and management extremely simple. There's a core Thematic function that looks for a folder called widgets in your child theme folder, and adds any widgets in there without any additional work on your part. This feature also means that your widgets will travel with your child theme, so if you're creating theme-specific widgets for distribution, you'll make things simpler for users by removing any extra plugin installation/activation steps. Another great aspect of using the widgets folder is that you can create each widget as a separate PHP file, which can help you stay organized.

In Chapter 5 we wrote a function to add author information to the end of every post. To make this functionality a little more flexible, why not turn it into a widget that will only display on single post pages? That way, your users can choose to display it in the sidebar, or elsewhere, or not at all.

The code that follows makes use of PHP's object oriented programming (OOP) features. If you're unfamiliar with object oriented programming, don't worry: I'll explain everything as simply as possible. This is just a warning that some of this code may seem a little strange to you if you've only ever worked with PHP in a procedural (or not object oriented) way.

Introducing the Widgets API

To create a new widget in WordPress, you extend the WP_Widget class. If this seems a bit beyond your grasp, don't worry: when developing WordPress widgets, it's unnecessary to think about objects and classes very much. Every widget class has the same four functions inside it, so you only need to write those four functions for every widget—and you can just copy and paste the basic layout of the class every time you want to make a new widget. The four functions you need are a constructor function (which always has the same name as the class itself), widget, update, and form.

Let's first have a look at the empty shell of a new widget:

class My_Widget extends WP_Widget { function My_Widget() {

// this is a constructor, where each new instance of your widget gets built

function form($instance) {

// this generates the widget options form which you see // in the widgets section in the admin

function update($new_instance, $old_instance) {

// this handles the submission of the options form to // update the widget options

function widget($args, $instance) {

// the most important function: this one handles actually outputting // the widget's content to the theme

Assuming you've filled out each of these functions with your desired functionality, there's only one step left. Much like widget-ready areas, new widgets need to be registered with WordPress:

register_widget('My_Widget');

At the most basic level, the form and update functions only need to give your users the option to input the title to be displayed with the widget. This means that unless you require more detailed options here, you can reuse the same code for those two functions for each widget you develop.

How To Become A Blogging Pro

How To Become A Blogging Pro

Save Hundreds Of Lost Hours and Thousands of Dollars in Lost Potential Earnings by Setting Your Blogs Up Right The First Time.

Get My Free Ebook


Post a comment