wp_register_widget_control()
Registers widget control callback for customizing options.
No Hooks.
Return
null
. Nothing (null).
Usage
wp_register_widget_control( $id, $name, $control_callback, $options, ...$params );
- $id(int|string) (required)
- Sidebar ID.
- $name(string) (required)
- Sidebar display name.
- $control_callback(callable) (required)
- Run when sidebar is displayed.
- $options(array)
Array or string of control options.
Default: empty array
-
height(int)
Never used.
Default: 200 -
width(int)
Width of the fully expanded control form (but try hard to use the default width).
Default: 250 - id_base(int|string)
Required for multi-widgets, i.e widgets that allow multiple instances such as the text widget. The widget ID will end up looking like {$id_base}-{$unique_number}.
-
- ...$params(mixed) (required)
- Optional additional parameters to pass to the callback function when it's called.
Examples
#1 Widget settings registration
We need to set the settings for the custom widget. The function which is responsible for outputting the widget's settings is registered as follows:
<?php // register widget settings wp_register_widget_control( 'my_widget', // id 'my_widget', // name 'my_widget_control', // name of the callback function array('width'=>500), // width of settings block $param1 = 'foo' // parameter passed to my_widget_control($param1) ); // widget settings form and save options function my_widget_control( $param1 = array() ) { // if the data is sent, save it in an option if( isset($_POST['submitted']) ) { update_option('my_widget_title', $_POST['widgettitle']); update_option('my_widget_twitterurl', $_POST['twitterurl']); update_option('my_widget_description', $_POST['description']); } // Load options $widgettitle = get_option('my_widget_title'); $twitterurl = get_option('my_widget_twitterurl'); $description = get_option('my_widget_description'); ?> Widget title:<br /> <input type="text" class="widefat" name="widgettitle" value="<?php echo stripslashes($widgettitle); ?>" /> <br /><br /> Description:<br /> <textarea class="widefat" rows="5" name="description"><?php echo stripslashes($description); ?></textarea> <br /><br /> Twitter profile (URL):<br /> <input type="text" class="widefat" name="twitterurl" value="<?php echo stripslashes($twitterurl); ?>" /> <br /><br /> <input type="hidden" name="submitted" value="1" /> <?php }
Notes
- Global. Array. $wp_registered_widget_controls The registered widget controls.
- Global. Array. $wp_registered_widget_updates The registered widget updates.
- Global. Array. $wp_registered_widgets The registered widgets.
- 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. |