Creating Your Own Widget

To create your own widget, you need to duplicate the widget class and add your own logic and form fields. Then you just need a few extra lines to register your widget and make it available on the widget manager screen. Listing 8-7 shows a simplified version of my Random Posts from Category plugin (Figure 8-2). For simplicity's sake, I've removed the extra code required for internationalizing the strings and supporting the the_excerpt Reloaded plugin as well as the built-in the_excerpt() function. Other than that, this really is the whole plugin. If you want, you can flip ahead to the next chapter and see how to set up the comment block necessary to use this as a plugin. Otherwise, paste this into your theme's functions.php file, and you'll see a new widget available under Appearance ^ Widgets.

The things that are unique to this widget are shown in bold.

Random Posts

Ttis is a sticky post!¡J Make sure it sticks!

Lorem Ipsum is simply dummy text of the printing and types etting industry. Lorem Ipsum has been the industiy's standard dummy text ever since the 1500s, when an unknown printer took a gailey ,.. Continue reading

Figure 8-2. The Random Posts from Category widget manager and output in the Twenty Ten theme

Listing 8-7. The Random Posts from Category widget class RandomPostsFromCategory extends WP_Widget {

Was this article helpful?

0 0

Post a comment