Adding Widgets

Widgets are individual features you can add to theme sidebars. Widgets can add a simple site search, display a calendar, list the most recent posts, and show RSS feed updates. These features just scratch the surface of what widgets offer and what widgets are capable of.

WordPress 2.8 introduced a new widget API that makes widget creation very easy. If you tried creating widgets in the past and gave up because of the difficulty of making them work properly, it's time to try widget creation again.

Coding a simple widget

The format for setting up filters and shortcodes are very similar. Add a function that performs a specific task and then use either add_filter or add_shortcode to register the function as a filter or shortcode.

Widgets are a bit different. Instead of creating one function and registering it, widgets are a collection of functions packaged in a container called a class. The class is registered as a widget using the register_widget function.

Like shortcodes, multiple plugins can be housed in a single plugin file. However, because the code for some of these widgets gets lengthy, each widget will be its own plugin.

The widget plugin we define in the following steps creates a widget that you can use on the Widgets page of your Dashboard (AppearanceimageWidgets) to show a widget in your sidebar that displays “You are logged in as Name” or “Welcome Guest” depending ...

Get WordPress® All-in-One For Dummies® now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.