Advertisements

dynamic_sidebar

Definition:
function dynamic_sidebar($index = 1) {}

Display dynamic sidebar.
By default it displays the default sidebar or ‘sidebar-1’. The ‘sidebar-1’ is not named by the theme, the actual name is ‘1’, but ‘sidebar-‘ is added to the registered sidebars for the name. If you named your sidebar ‘after-post’, then the parameter $index will still be ‘after-post’, but the lookup will be for ‘sidebar-after-post’.

Parameters

  • int|string $index: Optional, default is 1. Name or ID of dynamic sidebar.

Return values

returns:True, if widget sidebar was found and called. False if not found or not called.

Defined filters

  • dynamic_sidebar_params
    apply_filters( 'dynamic_sidebar_params', $params )

Defined actions

  • dynamic_sidebar
    do_action( 'dynamic_sidebar', $wp_registered_widgets[$id] );

Source code

function dynamic_sidebar($index = 1) {

	global $wp_registered_sidebars, $wp_registered_widgets;



	if ( is_int($index) ) {

		$index = "sidebar-$index";

	} else {

		$index = sanitize_title($index);

		foreach ( (array) $wp_registered_sidebars as $key => $value ) {

			if ( sanitize_title($value['name']) == $index ) {

				$index = $key;

				break;

			}

		}

	}



	$sidebars_widgets = wp_get_sidebars_widgets();

	if ( empty( $sidebars_widgets ) )

		return false;



	if ( empty($wp_registered_sidebars[$index]) || !array_key_exists($index, $sidebars_widgets) || !is_array($sidebars_widgets[$index]) || empty($sidebars_widgets[$index]) )

		return false;



	$sidebar = $wp_registered_sidebars[$index];



	$did_one = false;

	foreach ( (array) $sidebars_widgets[$index] as $id ) {



		if ( !isset($wp_registered_widgets[$id]) ) continue;



		$params = array_merge(

			array( array_merge( $sidebar, array('widget_id' => $id, 'widget_name' => $wp_registered_widgets[$id]['name']) ) ),

			(array) $wp_registered_widgets[$id]['params']

		);



		// Substitute HTML id and class attributes into before_widget

		$classname_ = '';

		foreach ( (array) $wp_registered_widgets[$id]['classname'] as $cn ) {

			if ( is_string($cn) )

				$classname_ .= '_' . $cn;

			elseif ( is_object($cn) )

				$classname_ .= '_' . get_class($cn);

		}

		$classname_ = ltrim($classname_, '_');

		$params[0]['before_widget'] = sprintf($params[0]['before_widget'], $id, $classname_);



		$params = apply_filters( 'dynamic_sidebar_params', $params );



		$callback = $wp_registered_widgets[$id]['callback'];



		do_action( 'dynamic_sidebar', $wp_registered_widgets[$id] );



		if ( is_callable($callback) ) {

			call_user_func_array($callback, $params);

			$did_one = true;

		}

	}



	return $did_one;

}

992

Advertisements

No comments yet... Be the first to leave a reply!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: