wp_render_widget( string $widget_id, string $sidebar_id ): string
- Since
- 5.8.0
- Source
wp-includes/widgets.php:2006
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
- Scaling
- Scales with input
- Instructions
- 7–77
- Plugin surface
- 2 hooks
- Called by
- 2
Touches nothing outside its own arguments.
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 92.
Third-party callbacks on 'dynamic_sidebar_params', 'dynamic_sidebar' run inside this call, and their cost is not bounded by anything here.
2 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 7–11 | none |
isset($wp_registered_widgets[$widget_id]) && !is_callable() | 70–73 | widget_id(), ltrim(), sprintf(), apply_filters(), ob_start(), do_action(), is_callable(), ob_get_clean() |
isset($wp_registered_widgets[$widget_id]) && is_callable() | 74–77 | widget_id(), ltrim(), sprintf(), apply_filters(), ob_start(), do_action(), is_callable(), call_user_func_array(), ob_get_clean() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 92 | 7–77 | 8 | |
| 8.5 | 92 | 7–77 | 8 | |
| 8.4 | 92 | 7–77 | 8 | |
| 8.3 | 92 | 7–77 | 8 | |
| 8.2 | 92 | 7–77 | 8 | |
| 8.1 | 92 | 7–77 | 8 | 1 more instruction than PHP 7.4 |
| 7.4 | 91 | 7–76 | 8 |
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:
- apply_filters( dynamic_sidebar_params )filterline 2047 (+41 into the body)
Filters the parameters passed to a widget's display callback.
- do_action( dynamic_sidebar )actionline 2054 (+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
- WP_REST_Widgets_Controller::prepare_item_for_response()Prepares the widget for the REST response.
- render_block_core_legacy_widget()Renders the 'core/legacy-widget' block.
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.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 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.