WP_Customize_Widgets::render_widget_partial() WordPress Method

This method is used internally by the Customizer to render a partial template for a given widget instance. This is used in the Customizer preview when changes are made to a widget form. The rendered back-end widget form is also used to populate the JS template for the widget control.

WP_Customize_Widgets::render_widget_partial( WP_Customize_Partial $partial, array $context ) #

Renders a specific widget using the supplied sidebar arguments.


Description

Top ↑

See also


Top ↑

Parameters

$partial

(WP_Customize_Partial)(Required)Partial.

$context

(array)(Required)Sidebar args supplied as container context.

  • 'sidebar_id'
    (string) ID for sidebar for widget to render into.
  • 'sidebar_instance_number'
    (int) Disambiguating instance number.


Top ↑

Return

(string|false)


Top ↑

Source

File: wp-includes/class-wp-customize-widgets.php

	public function render_widget_partial( $partial, $context ) {
		$id_data   = $partial->id_data();
		$widget_id = array_shift( $id_data['keys'] );

		if ( ! is_array( $context )
			|| empty( $context['sidebar_id'] )
			|| ! is_registered_sidebar( $context['sidebar_id'] )
		) {
			return false;
		}

		$this->rendering_sidebar_id = $context['sidebar_id'];

		if ( isset( $context['sidebar_instance_number'] ) ) {
			$this->context_sidebar_instance_number = (int) $context['sidebar_instance_number'];
		}

		// Filter sidebars_widgets so that only the queried widget is in the sidebar.
		$this->rendering_widget_id = $widget_id;

		$filter_callback = array( $this, 'filter_sidebars_widgets_for_rendering_widget' );
		add_filter( 'sidebars_widgets', $filter_callback, 1000 );

		// Render the widget.
		ob_start();
		$this->rendering_sidebar_id = $context['sidebar_id'];
		dynamic_sidebar( $this->rendering_sidebar_id );
		$container = ob_get_clean();

		// Reset variables for next partial render.
		remove_filter( 'sidebars_widgets', $filter_callback, 1000 );

		$this->context_sidebar_instance_number = null;
		$this->rendering_sidebar_id            = null;
		$this->rendering_widget_id             = null;

		return $container;
	}


Top ↑

Changelog

Changelog
VersionDescription
4.5.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More