wppaste
WordPress

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

Since
2.8.0
Source
wp-includes/widgets.php:1218
Outputs an arbitrary widget as a template tag.

Compatibility

WordPress
since 2.8.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$widgetstring
The widget's PHP class name (see class-wp-widget.php).
$instancearrayoptional
The widget's instance settings. Default empty array.Default: array()
$argsarrayoptional
Array of arguments to configure the display of the widget.Default: array()
  • $before_widgetstring

    HTML content that will be prepended to the widget's HTML output. Default <div class="widget %s">, where %s is the widget's class name.
  • $after_widgetstringdefault: &lt;/div&gt;

    HTML content that will be appended to the widget's HTML output.
  • $before_titlestringdefault: &lt;h2 class="widgettitle"&gt;

    HTML content that will be prepended to the widget's title when displayed.
  • $after_titlestringdefault: &lt;/h2&gt;

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

Performance profile

How much work a call to the_widget() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Trivial

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
12–53

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 68.

Plugin surface
2 hooks

Third-party callbacks on 'widget_display_callback', 'the_widget' run inside this call, and their cost is not bounded by anything here.

Called by
2

2 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()called directly

Further down the call graph this can also reach query, option, cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 4 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost the_widget() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
isset($widget) && !($widget_obj instanceof)12none
!isset($widget)20__(), sprintf(), _doing_it_wrong()
isset($widget) && $widget_obj instanceof && $instance === false40wp_parse_args(), sprintf(), wp_parse_args(), apply_filters()
isset($widget) && $widget_obj instanceof && $instance !== false53wp_parse_args(), sprintf(), wp_parse_args(), apply_filters(), do_action(), ->_set(), ->widget()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 68 instructions, 12–53 executed per call, 3 branches. The work does not change between versions.

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 2

2 hooks fire while the_widget() runs, in this order:

  1. apply_filters( widget_display_callback )filterline 1251 (+33 into the body)

    Filters the settings for a particular widget instance.

  2. do_action( the_widget )actionline 1266 (+48 into the body)

    Fires before rendering the requested widget.

Uses · 5

  • _doing_it_wrong()Marks something as being incorrectly called.
  • __()Retrieves the translation of $text.
  • wp_parse_args()Merges user defined arguments into defaults array.
  • apply_filters()Calls the callback functions that have been added to a filter hook.
  • do_action()Calls the callback functions that have been added to an action hook.

Used by · 2

Source code

function the_widget( $widget, $instance = array(), $args = array() ) {	global $wp_widget_factory; 	if ( ! isset( $wp_widget_factory->widgets[ $widget ] ) ) {		_doing_it_wrong(			__FUNCTION__,			sprintf(				/* translators: %s: register_widget() */				__( 'Widgets need to be registered using %s, before they can be displayed.' ),				'<code>register_widget()</code>'			),			'4.9.0'		);		return;	} 	$widget_obj = $wp_widget_factory->widgets[ $widget ];	if ( ! ( $widget_obj instanceof WP_Widget ) ) {		return;	} 	$default_args          = array(		'before_widget' => '<div class="widget %s">',		'after_widget'  => '</div>',		'before_title'  => '<h2 class="widgettitle">',		'after_title'   => '</h2>',	);	$args                  = wp_parse_args( $args, $default_args );	$args['before_widget'] = sprintf( $args['before_widget'], $widget_obj->widget_options['classname'] ); 	$instance = wp_parse_args( $instance ); 	/** This filter is documented in wp-includes/class-wp-widget.php */	$instance = apply_filters( 'widget_display_callback', $instance, $widget_obj, $args ); 	if ( false === $instance ) {		return;	} 	/**	 * Fires before rendering the requested widget.	 *	 * @since 3.0.0	 *	 * @param string $widget   The widget's class name.	 * @param array  $instance The current widget instance's settings.	 * @param array  $args     An array of the widget's sidebar arguments.	 */	do_action( 'the_widget', $widget, $instance, $args ); 	$widget_obj->_set( -1 );	$widget_obj->widget( $args, $instance );}

Changelog

Introduced in 2.8.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.9.7 tag, from src/wp-includes/widgets.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.