WordPress at a glance

wp_register_sidebar_widget() WP 1.0

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.

  • 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
  • Global. Array. $_wp_deprecated_widgets_callbacks
Hooks in function
Return

Nothing (null).

Usage

wp_register_sidebar_widget( $id, $name, $output_callback, $options );
$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.

  • $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.

Default: array()

Code of wp register sidebar widget: wp-includes/widgets.php VER 5.1

<?php
function wp_register_sidebar_widget( $id, $name, $output_callback, $options = array() ) {
	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'   => array_slice( func_get_args(), 4 ),
	);
	$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;
	}
}

Related Functions

From tag: Sidebar (widgets)

No comments
    Hello, !     Log In . Register