Using WordPress ‘dynamic_sidebar_params’ PHP filter

The dynamic_sidebar_params WordPress PHP filter allows you to modify the parameters passed to a widget’s display callback.

Usage

add_filter( 'dynamic_sidebar_params', 'my_custom_dynamic_sidebar_params' );

function my_custom_dynamic_sidebar_params( $params ) {
    // your custom code here
    return $params;
}

Parameters

  • $params (array): An array of widget display arguments, including:
    • name (string): Name of the sidebar the widget is assigned to.
    • id (string): ID of the sidebar the widget is assigned to.
    • description (string): The sidebar description.
    • class (string): CSS class applied to the sidebar container.
    • before_widget (string): HTML markup to prepend to each widget in the sidebar.
    • after_widget (string): HTML markup to append to each widget in the sidebar.
    • before_title (string): HTML markup to prepend to the widget title when displayed.
    • after_title (string): HTML markup to append to the widget title when displayed.
    • widget_id (string): ID of the widget.
    • widget_name (string): Name of the widget.
    • widget_args (array): An array of multi-widget arguments, including:
      • number (int): Number increment used for multiples of the same widget.

More information

See WordPress Developer Resources: dynamic_sidebar_params

Note: The filter is evaluated on both the front end and back end, including for the Inactive Widgets sidebar on the Widgets screen.

Examples

Change widget title tag

Change the widget title HTML tag from the default <h2> to <h3>:

add_filter( 'dynamic_sidebar_params', 'change_widget_title_tag' );

function change_widget_title_tag( $params ) {
    $params[0]['before_title'] = '<h3 class="widget-title">';
    $params[0]['after_title'] = '</h3>';
    return $params;
}

Add custom class to widget container

Add a custom CSS class to the widget container:

add_filter( 'dynamic_sidebar_params', 'add_custom_widget_class' );

function add_custom_widget_class( $params ) {
    $params[0]['before_widget'] = str_replace( 'class="', 'class="my-custom-class ', $params[0]['before_widget'] );
    return $params;
}

Change widget title for a specific widget

Change the title of a specific widget by its ID:

add_filter( 'dynamic_sidebar_params', 'change_specific_widget_title' );

function change_specific_widget_title( $params ) {
    if ( 'text-2' === $params[0]['widget_id'] ) {
        $params[0]['before_title'] = '<h4 class="custom-widget-title">';
        $params[0]['after_title'] = '</h4>';
    }
    return $params;
}

Add custom wrapper to widget content

Wrap widget content in a custom <div>:

add_filter( 'dynamic_sidebar_params', 'add_custom_wrapper_to_widget' );

function add_custom_wrapper_to_widget( $params ) {
    $params[0]['before_widget'] .= '<div class="custom-widget-wrapper">';
    $params[0]['after_widget'] = '</div>' . $params[0]['after_widget'];
    return $params;
}