wppaste
WordPress

wp_render_widget( string $widget_id, string $sidebar_id ): string

Since
5.8.0
Source
wp-includes/widgets.php:1990
Calls the render callback of a widget and returns the output.

Compatibility

WordPress
since 5.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

$widget_idstring
Widget ID.
$sidebar_idstring
Sidebar ID.

Return value

string

Performance profile

How much work a call to wp_render_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
Light

Touches nothing outside its own arguments.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
7–77

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

Plugin surface
2 hooks

Third-party callbacks on 'dynamic_sidebar_params', 'dynamic_sidebar' 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

What one call costs · 3 distinct outcomes

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

WhenInstructionsCalls it makes
always7–11none
isset($wp_registered_widgets[$widget_id]) && !is_callable()70–73widget_id(), ltrim(), sprintf(), apply_filters(), ob_start(), do_action(), is_callable(), ob_get_clean()
isset($wp_registered_widgets[$widget_id]) && is_callable()74–77widget_id(), ltrim(), sprintf(), apply_filters(), ob_start(), do_action(), is_callable(), call_user_func_array(), ob_get_clean()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev927–778
8.5927–778
8.4927–778
8.3927–778
8.2927–778
8.1927–7781 more instruction than PHP 7.4
7.4917–768

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 wp_render_widget() runs, in this order:

  1. apply_filters( dynamic_sidebar_params )filterline 2031 (+41 into the body)

    Filters the parameters passed to a widget's display callback.

  2. do_action( dynamic_sidebar )actionline 2038 (+48 into the body)

    Fires before a widget's display callback is called.

Uses · 2

  • 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 wp_render_widget( $widget_id, $sidebar_id ) {	global $wp_registered_widgets, $wp_registered_sidebars; 	if ( ! isset( $wp_registered_widgets[ $widget_id ] ) ) {		return '';	} 	if ( isset( $wp_registered_sidebars[ $sidebar_id ] ) ) {		$sidebar = $wp_registered_sidebars[ $sidebar_id ];	} elseif ( 'wp_inactive_widgets' === $sidebar_id ) {		$sidebar = array();	} else {		return '';	} 	$params = array_merge(		array(			array_merge(				$sidebar,				array(					'widget_id'   => $widget_id,					'widget_name' => $wp_registered_widgets[ $widget_id ]['name'],				)			),		),		(array) $wp_registered_widgets[ $widget_id ]['params']	); 	// Substitute HTML `id` and `class` attributes into `before_widget`.	$classname_ = '';	foreach ( (array) $wp_registered_widgets[ $widget_id ]['classname'] as $cn ) {		if ( is_string( $cn ) ) {			$classname_ .= '_' . $cn;		} elseif ( is_object( $cn ) ) {			$classname_ .= '_' . get_class( $cn );		}	}	$classname_                 = ltrim( $classname_, '_' );	$params[0]['before_widget'] = sprintf( $params[0]['before_widget'], $widget_id, $classname_ ); 	/** This filter is documented in wp-includes/widgets.php */	$params = apply_filters( 'dynamic_sidebar_params', $params ); 	$callback = $wp_registered_widgets[ $widget_id ]['callback']; 	ob_start(); 	/** This filter is documented in wp-includes/widgets.php */	do_action( 'dynamic_sidebar', $wp_registered_widgets[ $widget_id ] ); 	if ( is_callable( $callback ) ) {		call_user_func_array( $callback, $params );	} 	return ob_get_clean();}

Changelog

Introduced in 5.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.8.8 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.