wp_register_sidebar_widget()WP 2.2.0

Registers a widget that can then be added to the widget panel (sidebar) in the admin panel.

Hooks from the function

Returns

null. Does not return anything.

Usage

wp_register_sidebar_widget( $id, $name, $output_callback, $options, ...$params );
$id(string/int) (required)
Widget ID, a unique name. For example, your_widget_1.
$name(string) (required)
Widget title, by which it can be recognized in the admin panel.
$output_callback(string) (required)

The callback function that will be executed when the widget is called in the template.

Parameters passed to the callback function ($output_callback):

function my_output_callback_function( $args, $params ){ ... }
  • $args(array) (required)
    Widget parameters registered when registering the widget: widget_id, widget_name, etc.

  • $params(array) (required)
    Additional parameters passed to the function, the parameter $params of the wp_register_sidebar_widget() function.

The parameters are filtered by dynamic_sidebar_params before the callback function is called.

$options(string/array)

Widget settings. Example:

$widget_ops  = array(
	'classname'                   => 'widget_block',
	'description'                 => __( 'A widget containing a block.' ),
	'customize_selective_refresh' => true,
	'show_instance_in_rest'       => true, // since WP 5.8
	'mime_type'                   => '',
);

Default: array()

...$params(string/array/int/object/bool)
Parameters that will be added to the widget.

Examples

0

#1 Widget creation

The following code, will create a "My Widget" widget, which will become available in the WordPress admin panel. This widget can be dragged and dropped into the available widget bar (sidebar).

wp_register_sidebar_widget(
	'your_widget_1',       // widget ID
	'My widget',           // Widget header
	'your_widget_display', // Callback function
	// Settings
	array( 
		'description' => 'Widget description, what it`s for, what it does'
	)
);

function your_widget_display( $args ) {

   extract($args);
   echo $before_widget;

   echo $before_title . 'My unique widget' . $after_title;
   // Widget code, when displayed in a template
   echo "Your Widget Test";

   echo $after_widget;
}
0

#2 Registering a recursive widget

When registering a widget with wp_register_sidebar_widget(), the widget can only be used once, it can only be added to 1 sidebar. To register widgets that can be used multiple times, use register_widget():

class My_New_Widget extends WP_Widget {

	function My_New_Widget () {
		// An instance of the parent object
		parent::__construct( false, 'New widget header' );
	}

	function widget( $args, $instance ) {
		// Widget output on the screen
	}

	function update( $new_instance, $old_instance ) {
		// Saving widget settings
	}

	function form( $instance ) {
		// Widget settings form in the admin panel
	}
}

function register_My_New_Widget() {
	register_widget( 'My_New_Widget' );
}

add_action( 'widgets_init', 'register_My_New_Widget' );

Notes

  • Global. Array. $wp_registered_widgets Uses stored registered widgets.
  • Global. Array. $wp_registered_widget_controls Stores the registered widget controls (options).
  • Global. Array. $wp_registered_widget_updates The registered widget updates.
  • Global. Array. $_wp_deprecated_widgets_callbacks

Changelog

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

wp_register_sidebar_widget() code WP 6.9

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;
	}
}