wp_register_sidebar_widget()
Register an instance of a widget.
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.
Hooks from the function
Return
null
. Nothing (null).
Usage
wp_register_sidebar_widget( $id, $name, $output_callback, $options, ...$params );
- $id(int|string) (required)
- Widget ID.
- $name(string) (required)
- Widget display title.
- $output_callback(callable) (required)
- Run when widget is called.
- $options(array)
An array of supplementary widget options for the instance.
Default: array()
-
classname(string)
Class name for the widget's HTML container.
Default: 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(true|false)
Whether to show the widget's instance settings in the REST API. Only available for WP_Widget based widgets.
-
- ...$params(mixed) (required)
- Optional additional parameters to pass to the callback function when it's called.
Examples
#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; }
#2 Registering a recursive widget [auto-translate]
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. |