wp_register_widget_control()WP 2.2.0

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

0

#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.

wp_register_widget_control() code WP 6.5.2

function wp_register_widget_control( $id, $name, $control_callback, $options = array(), ...$params ) {
	global $wp_registered_widget_controls, $wp_registered_widget_updates, $wp_registered_widgets, $_wp_deprecated_widgets_callbacks;

	$id      = strtolower( $id );
	$id_base = _get_widget_id_base( $id );

	if ( empty( $control_callback ) ) {
		unset( $wp_registered_widget_controls[ $id ] );
		unset( $wp_registered_widget_updates[ $id_base ] );
		return;
	}

	if ( in_array( $control_callback, $_wp_deprecated_widgets_callbacks, true ) && ! is_callable( $control_callback ) ) {
		unset( $wp_registered_widgets[ $id ] );
		return;
	}

	if ( isset( $wp_registered_widget_controls[ $id ] ) && ! did_action( 'widgets_init' ) ) {
		return;
	}

	$defaults          = array(
		'width'  => 250,
		'height' => 200,
	); // Height is never used.
	$options           = wp_parse_args( $options, $defaults );
	$options['width']  = (int) $options['width'];
	$options['height'] = (int) $options['height'];

	$widget = array(
		'name'     => $name,
		'id'       => $id,
		'callback' => $control_callback,
		'params'   => $params,
	);
	$widget = array_merge( $widget, $options );

	$wp_registered_widget_controls[ $id ] = $widget;

	if ( isset( $wp_registered_widget_updates[ $id_base ] ) ) {
		return;
	}

	if ( isset( $widget['params'][0]['number'] ) ) {
		$widget['params'][0]['number'] = -1;
	}

	unset( $widget['width'], $widget['height'], $widget['name'], $widget['id'] );
	$wp_registered_widget_updates[ $id_base ] = $widget;
}