apply_filters( 'dynamic_sidebar_params', array $params )

Filters the parameters passed to a widget’s display callback.


Description Description

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

See also See also


Top ↑

Parameters Parameters

$params

(array)

  • 'args'
    (array) An array of widget display arguments.
    • '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.
    • 'number'
      (int) Number increment used for multiples of the same widget.


Top ↑

Source Source

File: wp-includes/widgets.php

View on Trac



Top ↑

Changelog Changelog

Changelog
Version Description
2.5.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by Razon

    Firstly we create a custom widget field end of the each widgets control form.

    if ( ! function_exists( 'wpdocs_display_custom_field_in_widget_form' ) ) {
    
        add_action( 'in_widget_form', 'wpdocs_display_custom_field_in_widget_form', 10, 3 );
    
        /**
         * Append custom field end of the widgets control form
         * Fires at the end of the widget control form.
         */
        function wpdocs_display_custom_field_in_widget_form( $widget, $return, $instance ) {
            $column = isset( $instance['column'] ) ? $instance['column'] : '';
    
            ob_start(); 
            ?>
    
            <p>
                <label for="<?php echo esc_attr( $widget->get_field_id( 'itclan_bs_grid_class' ) ) ?>"><?php _e( 'Column Label', 'text_domain' ) ?>
                    <input class="widefat" value="<?php echo esc_attr( $column ) ?>" id="<?php echo esc_attr( $widget->get_field_id( 'column' ) ) ?>" name="<?php echo esc_attr( $widget->get_field_name( 'column' ) ) ?>" type="text" />
                    <small><?php _e( 'Some additional description.', 'text_domain' ) ?></small>
                </label>
            </p>
    
            <?php 
            echo ob_get_clean();
        }
    }

    Now, we filter widget’s settings before saving. It will save our custom field column data.

    if ( ! function_exists( 'wpdocs_update_custom_field_in_widget_form' ) ) {
    
        add_action( 'widget_update_callback', 'wpdocs_update_custom_field_in_widget_form', 10, 2 );
    
        /**
         * Update widget fields
         * Filters a widget’s settings before saving.
         */
        function wpdocs_update_custom_field_in_widget_form( $instance, $new_instance ) {
            $instance['column'] = !empty( $new_instance['column'] ) ? $new_instance['column'] : '';
            return $instance;
        }
    }

    Finally, filter sidebar params for frontend. It will add class to before_widget from column field

    if ( ! function_exists( 'wpdocs_filter_dynamic_sidebar_params' ) ) {
    
        add_action( 'dynamic_sidebar_params', 'wpdocs_filter_dynamic_sidebar_params' );
    
        /**
         * Update widget fields
         * Filters the parameters passed to a widget’s display callback.
         */
        function wpdocs_filter_dynamic_sidebar_params( $params ) {
            global $wp_registered_widgets;
            $widget_id        = $params[0]['widget_id'];
            $widget_obj       = $wp_registered_widgets[ $widget_id ];
            $widget_opt       = get_option( $widget_obj['callback'][0]->option_name );
            $widget_num       = $widget_obj['params'][0]['number'];
            $grid_class       = isset( $widget_opt[ $widget_num ]['column'] ) ? $widget_opt[ $widget_num ]['column'] : '';
    
            if ( preg_match( '/class="/', $params[0]['before_widget'] ) && $grid_class ) {
                $params[0]['before_widget'] = preg_replace( '/class="/', "class=\"{$grid_class} ", $params[0]['before_widget'], 1 );
            }
    
            return $params;
        }
    }

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