wp_register_sidebar_widget() WordPress Function
The wp_register_sidebar_widget() function allows you to register a new sidebar widget. You will need to supply the following parameters: 1. The id of the sidebar widget. 2. The name of the sidebar widget. 3. The callback function that will display the sidebar widget content. Once you have registered a sidebar widget, it will appear in the WordPress admin backend under the "Widgets" section. You can then drag and drop the widget into any sidebar on your website.
wp_register_sidebar_widget( int|string $id, string $name, callable $output_callback, array $options = array(), mixed $params ) #
Register 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.
Parameters
- $id
(int|string)(Required)Widget ID.
- $name
(string)(Required)Widget display title.
- $output_callback
(callable)(Required)Run when widget is called.
- $options
(array)(Optional)An array of supplementary widget options for the instance.
- 'classname'
(string) Class name for the widget's HTML container. Default is a shortened version of the output callback name. - 'description'
(string) Widget description for display in the widget administration panel and/or theme. - 'show_instance_in_rest'
(bool) Whether to show the widget's instance settings in the REST API. Only available for WP_Widget based widgets.
Default value: array()
- 'classname'
- $params
(mixed)(Optional)additional parameters to pass to the callback function when it's called.
Source
File: wp-includes/widgets.php
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;
}
}
Expand full source codeCollapse full source codeView on TracView on GitHub
Changelog
| Version | Description |
|---|---|
| 5.8.0 | Added show_instance_in_rest option. |
| 5.3.0 | Formalized the existing and already documented ...$params parameter by adding it to the function signature. |
| 2.2.0 | Introduced. |