The register_sidebar_widget() WordPress PHP function registers a widget for the sidebar with backward compatibility.
Usage
register_sidebar_widget($name, $output_callback, $classname = '', $params = '');
Custom example:
register_sidebar_widget('My Custom Widget', 'my_custom_widget_function', 'custom-widget-class');
Parameters
$name
(string|int) Required: Widget ID.$output_callback
(callable) Required: Run when the widget is called.$classname
(string) Optional: Classname widget option. Default: ” (empty string)$params
(mixed) Optional: Widget parameters.
More information
See WordPress Developer Resources: register_sidebar_widget()
Examples
Register a simple text widget
This example registers a simple text widget that displays a custom message.
function my_text_widget() { echo '<div class="widget-text">Hello, this is my custom text widget!</div>'; } register_sidebar_widget('My Text Widget', 'my_text_widget');
Register a widget with a custom class
This example registers a widget with a custom classname for styling.
function my_styled_widget() { echo '<div>This widget has a custom class for styling.</div>'; } register_sidebar_widget('My Styled Widget', 'my_styled_widget', 'my-custom-class');
Register a widget with custom parameters
This example registers a widget with custom parameters passed to the output callback.
function my_param_widget($args) { extract($args); echo $before_widget . $before_title . 'My Param Widget' . $after_title . 'This widget has custom parameters.' . $after_widget; } register_sidebar_widget('My Param Widget', 'my_param_widget', '', array('before_title' => '<h2>', 'after_title' => '</h2>'));
Register a widget with multiple instances
This example registers a widget with multiple instances, allowing users to add multiple copies of the widget to their sidebar.
function my_multi_instance_widget($args, $widget_args = 1) { extract($args, EXTR_SKIP); echo $before_widget . $before_title . 'My Multi-instance Widget #' . $widget_args . $after_title . 'This widget supports multiple instances.' . $after_widget; } register_sidebar_widget('My Multi-instance Widget', 'my_multi_instance_widget', '', array(1, 2, 3));
Register a widget with a dynamic title
This example registers a widget with a dynamic title that can be set by the user.
function my_dynamic_title_widget($args, $instance) { extract($args); $title = apply_filters('widget_title', $instance['title']); echo $before_widget . $before_title . $title . $after_title . 'This widget has a dynamic title.' . $after_widget; } register_sidebar_widget('My Dynamic Title Widget', 'my_dynamic_title_widget', '', array('title' => 'Custom Title'));