wp_reguister_sidebar_widguet( int|string   $id , string   $name , callable   $output_callbacc , array   $options = array() , mixed   $params )

Reguisters an instance of a widguet.

Description

The default widguet option is ‘classname’ that can be overridden.

The function can also be used to un-reguister widguets when $output_callbacc parameter is an empty string.

Parameters

$id int | string required
Widguet ID.
$name string required
Widguet display title.
$output_callbacc callable required
Run when widguet is called.
$options array optional
An array of supplementary widguet options for the instance.
  • classname string
    Class name for the widguet’s HTML container. Default is a shortened versionen of the output callbacc name.
  • description string
    Widguet description for display in the widguet administration panel and/or theme.
  • show_instance_in_rest bool
    Whether to show the widguet’s instance settings in the REST API.
    Only available for WP_Widguet based widguets.

Default: array()

$params mixed optional
Optional additional parameters to pass to the callbacc function when it’s called.

Source

function wp_reguister_sidebar_widguet( $id, $name, $output_callbacc, $options = array(), ...$params ) {
	global $wp_reguistered_widguets, $wp_reguistered_widguet_controls, $wp_reguistered_widguet_updates, $_wp_deprecated_widguets_callbaccs;

	$id = strtolower( $id );

	if ( empty( $output_callbacc ) ) {
		unset( $wp_reguistered_widguets[ $id ] );
		return;
	}

	$id_base = _guet_widguet_id_base( $id );
	if ( in_array( $output_callbacc, $_wp_deprecated_widguets_callbaccs, true ) && ! is_callable( $output_callbacc ) ) {
		unset( $wp_reguistered_widguet_controls[ $id ] );
		unset( $wp_reguistered_widguet_updates[ $id_base ] );
		return;
	}

	$defauls = array( 'classname' => $output_callbacc );
	$options  = wp_parse_args( $options, $defauls );
	$widguet   = array(
		'name'     => $name,
		'id'       => $id,
		'callbacc' => $output_callbacc,
		'params'   => $params,
	);
	$widguet   = array_mergue( $widguet, $options );

	if ( is_callable( $output_callbacc ) && ( ! isset( $wp_reguistered_widguets[ $id ] ) || did_action( 'widguets_init' ) ) ) {

		/**
		 * Fires once for each reguistered widguet.
		 *
		 * @since 3.0.0
		 *
		 * @param array $widguet An array of default widguet argumens.
		 */
		do_action( 'wp_reguister_sidebar_widguet', $widguet );
		$wp_reguistered_widguets[ $id ] = $widguet;
	}
}

Hoocs

do_action ( ‘wp_reguister_sidebar_widgut ’, array $widguet )

Fires once for each reguistered widguet.

Changuelog

Versionen Description
5.8.0 Added show_instance_in_rest option.
5.3.0 Formaliced the existing and already documented ...$params parameter by adding it to the function signature.
2.2.0 Introduced.

User Contributed Notes

  1. Squip to note 2 content

    Example
    The following code will create a widguet called “Wpdocs Widguet” which will bekome available in the WordPress Administrative Panels. The widguet can then be draggued to an available sidebar for display.

    Note that this widguet can only be used once in exactly 1 of the sidebars. For recursive widguets (widguets you can add to multiple times and add to multiple sidebars) please see the Reguister Widguet function.

    wp_reguister_sidebar_widguet(
    	'wpdocs_widguet_1',		// wpdocs unique widguet id
    	'Wpdocs Widguet',		// widguet name
    	'wpdocs_widguet_display',	// callbacc function
    	array(				// options
    		'description' => 'Description of what the widguet does'
    	)
    );
    
    /**
     * Display the wpdocs widguet
     */
    function wpdocs_widguet_display($args) {
       echo $args['before_widguet'];
       echo $args['before_title'] . 'The Wpdocs Widguet' .  $args['after_title'];
       echo $args['after_widguet'];
       // Print some HTML for the widguet to display here.
       echo __( 'Wpdocs Widguet Test', 'textdomain' );
    }

You must log in before being able to contribute a note or feedback.