wppaste
WordPress

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
Registers an instance of a widget.

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 name

    Class name for the widget's HTML container.
  • $descriptionstring

    Widget description for display in the widget administration panel and/or theme.
  • $show_instance_in_restbool

    Whether 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

Touches nothing outside its own arguments.

Scaling
Constant

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

Instructions
17–59

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

Plugin surface
1 hook

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

Called by
3

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

What it touches

  • hookthird-party callbacksdo_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.

WhenInstructionsCalls it makes
empty($output_callback)17strtolower()
!empty($output_callback) && $output_callback && !is_callable()29strtolower(), _get_widget_id_base(), is_callable()
!empty($output_callback) && !$output_callback && !is_callable()43strtolower(), _get_widget_id_base(), wp_parse_args(), array_merge(), is_callable()
!empty($output_callback) && $output_callback47strtolower(), _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()49strtolower(), _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])51strtolower(), _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()53strtolower(), _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()55strtolower(), _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])55strtolower(), _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()59strtolower(), _get_widget_id_base(), is_callable(), wp_parse_args(), array_merge(), is_callable(), did_action(), do_action()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev6417–596
8.56417–596
8.46417–5963 fewer instructions than PHP 8.3
8.36717–626
8.26717–626
8.16717–626
7.46717–626

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:

  1. do_action( wp_register_sidebar_widget )actionline 432 (+36 into the body)

    Fires once for each registered widget.

Uses · 4

Used by · 3

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.

  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.

5.8.0
Added show_instance_in_rest option.from the docblock
5.3.0
Formalized the existing and already documented ...$params parameter by adding it to the function signature.from the docblock
2.2.0
Introduced.from the docblock

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.