Make WordPress Core

Changeset 11317


Ignore:
Timestamp:
05/13/2009 08:43:49 PM (15 years ago)
Author:
azaozz
Message:

Add a function to output a generic widget anywhere in a template, props Denis-de-Bernardy, fixes #9701

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/widgets.php

    r11310 r11317  
    11911191    return wp_unregister_widget_control($id);
    11921192}
     1193
     1194/**
     1195 * Output an arbitrary widget as a template tag
     1196 *
     1197 * @since 2.8
     1198 *
     1199 * @param string $widget the widget's PHP class name (see default-widgets.php)
     1200 * @param array $instance the widget's instance settings
     1201 * @param array $args the widget's sidebar args
     1202 * @return void
     1203 **/
     1204
     1205function the_widget($widget, $instance = array(), $args = array()) {
     1206    global $wp_widget_factory;
     1207
     1208    $widget_obj = $wp_widget_factory->widgets[$widget];
     1209    if ( !is_a($widget_obj, 'WP_Widget') )
     1210        return;
     1211
     1212    $before_widget = sprintf('<div class="widget %s">', $widget_obj->widget_options['classname']);
     1213    $default_args = array('before_widget' => $before_widget, 'after_widget' => "</div>", 'before_title' => '<h2 class="widgettitle">', 'after_title' => '</h2>');
     1214
     1215    $args = wp_parse_args($args, $default_args);
     1216    $instance = wp_parse_args($instance);
     1217
     1218    $widget_obj->_set(-1);
     1219    $widget_obj->widget($args, $instance);
     1220}
Note: See TracChangeset for help on using the changeset viewer.