dynamic_sidebar_paramsfilter-hookWP 2.5.0

Allows you to change the parameters passed to a widget's display callback.

The filter fires both on the front end and in the admin area, including for inactive sidebar widgets on the Widgets screen.

Usage

add_filter( 'dynamic_sidebar_params', 'wp_kama_dynamic_sidebar_params_filter' );

/**
 * Function for `dynamic_sidebar_params` filter-hook.
 * 
 * @param array $params 
 *
 * @return array
 */
function wp_kama_dynamic_sidebar_params_filter( $params ){
	// filter...
	return $params;
}
$params(array)
  • args(array)
    Array of widget display arguments.

    • name(string)
      Name of the sidebar to which the widget is assigned.

    • id(string)
      ID of the sidebar to which the widget is assigned.

    • description(string)
      Sidebar description.

    • class(string)
      Value of the class attribute for the sidebar container.

    • before_widget(string)
      Text before each widget in the sidebar.

    • after_widget(string)
      Text after each widget in the sidebar.

    • before_title(string)
      Text before the widget title.

    • after_title(string)
      Text after the widget title.

    • widget_id(string)
      Widget ID.

    • widget_name(string)
      Widget name.
  • widget_args(array)
    Array of arguments for multi-widgets.

    • number(int)
      Instance number when the same widget is used multiple times.

Examples

#1 Replace the container class with the widget name

add_filter( 'dynamic_sidebar_params', 'my_edit_widget_func' );

function my_edit_widget_func( $params ) {
	$class = str_replace( ' ', '-', $params[0]['widget_name'] );
	$params[0]['before_title'] = '<h3 class="' . $class . '">' ;

	return $params;
}

#2 Add an image before the title of a particular widget

add_filter( 'dynamic_sidebar_params', 'my_edit_widget_func' );

function my_edit_widget_func( $params ) {
	if ( $params[0]['widget_id'] == 'example-1' ) {
		$params[0]['before_title'] .= '<img src="' . get_stylesheet_directory_uri() . '/example-icon.png">';
	}

	return $params;
}

#3 Change the container class for even-numbered widgets

add_filter( 'dynamic_sidebar_params', 'change_class_for_even' );

function change_class_for_even( $params ) {
	global $widget_counter;

	if ( $params[0]['id'] == 'sidebar-1' ) {
		$widget_counter++;
		if ( $widget_counter % 2 == 0 ) {
			$params[0]['before_widget'] = '<section id="' . $params[0]['widget_id'] . '" class="widget-even">';
		}
	}
	return $params;
}

Changelog

Since 2.5.0 Introduced.

Where the hook is called

dynamic_sidebar()
dynamic_sidebar_params
wp_render_widget()
dynamic_sidebar_params
wp-includes/widgets.php 815
$params = apply_filters( 'dynamic_sidebar_params', $params );
wp-includes/widgets.php 2047
$params = apply_filters( 'dynamic_sidebar_params', $params );

Where the hook is used in WordPress

wp-admin/includes/widgets.php 88
add_filter( 'dynamic_sidebar_params', 'wp_list_widget_controls_dynamic_sidebar' );
wp-includes/class-wp-customize-widgets.php 1800
add_filter( 'dynamic_sidebar_params', array( $this, 'filter_dynamic_sidebar_params' ) );