Posts Tagged ‘Wordpress’

Wordpress widgets internals – Part 1

Thursday, November 20th, 2008

Wordpress 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' );

How to work with Wordpress blog settings

Thursday, October 16th, 2008

Wordpress blog settings are stored in the wp_options database table and can be read with the get_option($setting) function. This function applies pre_option_{setting} filters, checks whether the option is already loaded, and returns its value by fetching it from the database or from the cache.

Wordpress has more then 90 options but most frequently used of them are the following (in alphabet order):

 

  • blog_charset
  • blogname
  • date_format
  • default_category
  • default_ping_status
  • gmt_offset
  • home
  • permalink_structure
  • siteurl
  • time_format

There are several functions for managing options: update_option($name, $value), add_option($name, $value, $depricated, autoload), delete_option($name).

Most of the options can be modified in wp-admin. For example, the “Front page displays” option that specifies whether front page should display list of latest posts or specific page can be configured on the “Reading Settings” page in the wp-admin/options-reading.php file. After modifying, this setting is saved by the wp-admin/options.php file in the show_on_front, page_on_front, and page_for_posts options.

With this information we can create a simple function that checks whether the current page is the front page of a Wordpress site:

function is_front_page2() {
  if (’page’ == get_option(’show_on_front’)
    && 0 != ($id = get_option(’page_on_front’))
    && null != ($page = get_page($id)))
    return is_page($page->ID);
  return is_home();
}

is_front_page2 was chosen because the is_front_page function exists, but it returns wrong result when show_on_front = ‘page’ and page_on_front == 0 and page_for_posts != 0.