wp_register_sidebar_widget( int|string $id, string $name, callable $output_callback, array $options = array(), mixed $params )
- Since
- 2.2.0, 5.3.0, 5.8.0
- Source
wp-includes/widgets.php:396
Description
The default widget option is 'classname' that can be overridden.
The function can also be used to un-register widgets when $output_callback parameter is an empty string.
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
$idint|string- Widget ID.
$namestring- Widget display title.
$output_callbackcallable- Run when widget is called.
$optionsarrayoptional- An array of supplementary widget options for the instance.Default:
array()$classnamestringdefault: is a shortened version of the output callback nameClass name for the widget's HTML container.$descriptionstringWidget description for display in the widget administration panel and/or theme.$show_instance_in_restboolWhether to show the widget's instance settings in the REST API. Only available for WP_Widget based widgets.
$paramsmixed- Optional additional parameters to pass to the callback function when it's called.
Performance profile
How much work a call to wp_register_sidebar_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
- Scaling
- Constant
- Instructions
- 17–59
- Plugin surface
- 1 hook
- Called by
- 3
Touches nothing outside its own arguments.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 64.
Third-party callbacks on 'wp_register_sidebar_widget' run inside this call, and their cost is not bounded by anything here.
3 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
do_action()called directly
What one call costs · 10 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_register_sidebar_widget() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
empty($output_callback) | 17 | strtolower() |
!empty($output_callback) && $output_callback && !is_callable() | 29 | strtolower(), _get_widget_id_base(), is_callable() |
!empty($output_callback) && !$output_callback && !is_callable() | 43 | strtolower(), _get_widget_id_base(), wp_parse_args(), array_merge(), is_callable() |
!empty($output_callback) && $output_callback | 47 | strtolower(), _get_widget_id_base(), is_callable(), wp_parse_args(), array_merge(), is_callable() |
!empty($output_callback) && !$output_callback && is_callable() && isset($wp_registered_widgets[$id]) && !did_action() | 49 | strtolower(), _get_widget_id_base(), wp_parse_args(), array_merge(), is_callable(), did_action() |
!empty($output_callback) && !$output_callback && is_callable() && !isset($wp_registered_widgets[$id]) | 51 | strtolower(), _get_widget_id_base(), wp_parse_args(), array_merge(), is_callable(), do_action() |
!empty($output_callback) && $output_callback && is_callable() && isset($wp_registered_widgets[$id]) && !did_action() | 53 | strtolower(), _get_widget_id_base(), is_callable(), wp_parse_args(), array_merge(), is_callable(), did_action() |
!empty($output_callback) && !$output_callback && is_callable() && isset($wp_registered_widgets[$id]) && did_action() | 55 | strtolower(), _get_widget_id_base(), wp_parse_args(), array_merge(), is_callable(), did_action(), do_action() |
!empty($output_callback) && $output_callback && is_callable() && !isset($wp_registered_widgets[$id]) | 55 | strtolower(), _get_widget_id_base(), is_callable(), wp_parse_args(), array_merge(), is_callable(), do_action() |
!empty($output_callback) && $output_callback && is_callable() && isset($wp_registered_widgets[$id]) && did_action() | 59 | strtolower(), _get_widget_id_base(), is_callable(), wp_parse_args(), array_merge(), is_callable(), did_action(), do_action() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 64 | 17–59 | 6 | |
| 8.5 | 64 | 17–59 | 6 | |
| 8.4 | 64 | 17–59 | 6 | 3 fewer instructions than PHP 8.3 |
| 8.3 | 67 | 17–62 | 6 | |
| 8.2 | 67 | 17–62 | 6 | |
| 8.1 | 67 | 17–62 | 6 | |
| 7.4 | 67 | 17–62 | 6 |
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 · 1
One hook fires while wp_register_sidebar_widget() runs, in this order:
- do_action( wp_register_sidebar_widget )actionline 432 (+36 into the body)
Fires once for each registered widget.
Uses · 4
- _get_widget_id_base()Retrieves the widget ID base value.
- wp_parse_args()Merges user defined arguments into defaults array.
- did_action()Retrieves the number of times an action has been fired during the current request.
- do_action()Calls the callback functions that have been added to an action hook.
Used by · 3
- WP_Widget::_register_one()Registers an instance of the widget class.
- register_sidebar_widget()Register widget for sidebar with backward compatibility.
- wp_unregister_sidebar_widget()Remove widget from sidebar.
Source code
function wp_register_sidebar_widget( $id, $name, $output_callback, $options = array(), ...$params ) { global $wp_registered_widgets, $wp_registered_widget_controls, $wp_registered_widget_updates, $_wp_deprecated_widgets_callbacks; $id = strtolower( $id ); if ( empty( $output_callback ) ) { unset( $wp_registered_widgets[ $id ] ); return; } $id_base = _get_widget_id_base( $id ); if ( in_array( $output_callback, $_wp_deprecated_widgets_callbacks, true ) && ! is_callable( $output_callback ) ) { unset( $wp_registered_widget_controls[ $id ] ); unset( $wp_registered_widget_updates[ $id_base ] ); return; } $defaults = array( 'classname' => $output_callback ); $options = wp_parse_args( $options, $defaults ); $widget = array( 'name' => $name, 'id' => $id, 'callback' => $output_callback, 'params' => $params, ); $widget = array_merge( $widget, $options ); if ( is_callable( $output_callback ) && ( ! isset( $wp_registered_widgets[ $id ] ) || did_action( 'widgets_init' ) ) ) { /** * Fires once for each registered widget. * * @since 3.0.0 * * @param array $widget An array of default widget arguments. */ do_action( 'wp_register_sidebar_widget', $widget ); $wp_registered_widgets[ $id ] = $widget; }}Changelog
Introduced in 2.2.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
...$params parameter by adding it to the function signature.from the docblockAbout 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.