Wordpress widgets internals – Part 1
Thursday, November 20th, 2008Wordpress widgets are the reusable information blocks that admin can place in one of the theme sidebars. The widget can display any information: RSS feed records, Twitter account events, Facebook board events, stock quotes, geography of visitors, polls, etc.
Technically, Wordpress widget is a PHP function that may perform a variety of steps to produce the HTML fragment that will be inserted to sidebar. Sidebar calls these widget functions in the order and with parameters defined in administration part of the Wordpress site.
For example, let’s open the sidebar.php of the default Wordpress theme and analyze its content:
<div id="sidebar">
<ul>
<?php /* Widgetized sidebar, if you have the plugin installed. */
if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar() ) : ?>
<li>
<?php include (TEMPLATEPATH . '/searchform.php'); ?>
</li>
...
<?php endif; ?>
</ul>
</div>
The first if checks for existence of the dynamic_sidebar function and calls it. This function prints the widgets and returns true when the sidebar contains widgets and false otherwise. If the sidebar does not contain the widgets the default sidebar content, the if statement body, is displayed.
The dynamic_sidebar function calls the wp_get_sidebars_widgets function and iterate over its result, the list of sidebar widgets, calling callback function of the widgets.
The following table shows the list of default Wordpress widgets and their callback functions:
| Widget | Callback function | Description |
|---|---|---|
| Pages | wp_widget_pages | Your blog’s WordPress Pages |
| Calendar | wp_widget_calendar | A calendar of your blog’s posts |
| Archives | wp_widget_archives | A monthly archive of your blog’s posts |
| Links | wp_widget_links | Your blogroll |
| Meta | wp_widget_meta | Log in/out, admin, feed and WordPress links |
| Search | wp_widget_search | A search form for your blog |
| Recent Posts | wp_widget_recent_entries | The most recent posts on your blog |
| Tag Cloud | wp_widget_tag_cloud | Your most used tags in cloud format |
| Categories | wp_widget_categories | A list or dropdown of categories |
| Text | wp_widget_text | Arbitrary text or HTML |
| RSS | wp_widget_rss | Entries from any RSS or Atom feed |
| Recent Comments | wp_widget_recent_comments | The most recent comments |
Information about available (or registered) widgets is stored in the $wp_registered_widgets global array variable and other functions query this variable to fetch the list of available widgets and get information about the widgets.
The widget can be registered with the wp_register_sidebar_widget function. This function calls the widgets_init action and modifies the $wp_registered_widgets array.
Here is the sample code fragments extracted from Wordpress that registers several widgets using the wp_register_sidebar_widget function:
The default Wordpress widgets are registered in the wp_widgets_init function. The following code fragment is extracted from this function:
$widget_ops = array('classname' => 'widget_pages', 'description' =>
__( "Your blog's WordPress Pages") );
wp_register_sidebar_widget('pages', __('Pages'), 'wp_widget_pages', $widget_ops);
wp_register_widget_control('pages', __('Pages'), 'wp_widget_pages_control' );
$widget_ops = array('classname' => 'widget_calendar', 'description' =>
__( "A calendar of your blog's posts") );
wp_register_sidebar_widget('calendar', __('Calendar'), 'wp_widget_calendar', $widget_ops);
wp_register_widget_control('calendar', __('Calendar'),
'wp_widget_calendar_control' );