the_widget

Output an arbitrary widget as a template tag.

Description

the_widget( (string) $widget, (array) $instance = array(), (array) $args = array() ); 

Parameters (3)

0. $widget (string)
The widget's PHP class name (see class-wp-widget.php).
1. $instance — Optional. (array) => array()
The widget's instance settings. Default empty array.
2. $args — Optional. (array) => array()
Array of arguments to configure the display of the widget.

Options

  • before_widget (string) => ', where %s is the widget'

    HTML content that will be prepended to the widget's HTML output.

  • after_widget (string) => ''

    HTML content that will be appended to the widget's HTML output.

  • before_title (string) => ''

    HTML content that will be prepended to the widget's title when displayed.

array(

    /**
     * HTML content that will be prepended to the widget's HTML output.
     *
     * @type string
     * @default ', where %s is the widget'
     */
    'before_widget' => ', where %s is the widget',

    /**
     * HTML content that will be appended to the widget's HTML output.
     *
     * @type string
     * @default ''
     */
    'after_widget' => '',

    /**
     * HTML content that will be prepended to the widget's title when displayed.
     *
     * @type string
     * @default ''
     */
    'before_title' => ''
);        


Usage

  1. if ( !function_exists( 'the_widget' ) ) { 
  2. require_once ABSPATH . WPINC . '/widgets.php'; 
  3.  
  4. // The widget's PHP class name (see class-wp-widget.php). 
  5. $widget = ''; 
  6.  
  7. // Optional. The widget's instance settings. Default empty array. 
  8. $instance = array(); 
  9.  
  10. // Optional. Array of arguments to configure the display of the widget. 
  11. $args = array( 
  12. 'before_widget' => ', where %s is the widget', 
  13. 'after_widget' => '', 
  14. 'before_title' => '' 
  15. ); 
  16.  
  17. // NOTICE! Understand what this does before running. 
  18. $result = the_widget($widget, $instance, $args); 
  19.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/widgets.php  
  1. function the_widget( $widget, $instance = array(), $args = array() ) { 
  2. global $wp_widget_factory; 
  3.  
  4. $widget_obj = $wp_widget_factory->widgets[$widget]; 
  5. if ( ! ( $widget_obj instanceof WP_Widget ) ) { 
  6. return; 
  7.  
  8. $default_args = array( 
  9. 'before_widget' => '<div class="widget %s">',  
  10. 'after_widget' => "</div>",  
  11. 'before_title' => '<h2 class="widgettitle">',  
  12. 'after_title' => '</h2>',  
  13. ); 
  14. $args = wp_parse_args( $args, $default_args ); 
  15. $args['before_widget'] = sprintf( $args['before_widget'], $widget_obj->widget_options['classname'] ); 
  16.  
  17. $instance = wp_parse_args($instance); 
  18.  
  19. /** 
  20. * Fires before rendering the requested widget. 
  21. * @since 3.0.0 
  22. * @param string $widget The widget's class name. 
  23. * @param array $instance The current widget instance's settings. 
  24. * @param array $args An array of the widget's sidebar arguments. 
  25. */ 
  26. do_action( 'the_widget', $widget, $instance, $args ); 
  27.  
  28. $widget_obj->_set(-1); 
  29. $widget_obj->widget($args, $instance);