хук-фильтр since 2.5.0

dynamic_sidebar_params

Проверено на WordPress 6.9, обновлено Источник: WordPress Developer Resources.

Сигнатура

apply_filters( 'dynamic_sidebar_params', array $params )

Описание

Примечание: фильтр применяется как в публичной части сайта, так и в административной, включая сайдбар «Неактивные виджеты» на экране «Виджеты».
См. alsoregister_sidebar()

Оригинал (английский)

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

Фильтруемое значение

  • args array An array of widget display arguments.
    • name stringName of the sidebar the widget is assigned to.
    • id stringID of the sidebar the widget is assigned to.
    • description stringThe sidebar description.
    • class stringCSS class applied to the sidebar container.
    • before_widget stringHTML markup to prepend to each widget in the sidebar.
    • after_widget stringHTML markup to append to each widget in the sidebar.
    • before_title stringHTML markup to prepend to the widget title when displayed.
    • after_title stringHTML markup to append to the widget title when displayed.
    • widget_id stringID of the widget.
    • widget_name stringName of the widget.
    • widget_args array An array of multi-widget arguments.
      • number intNumber increment used for multiples of the same widget.
      Source
      $params = apply_filters( 'dynamic_sidebar_params', $params );
      

      View all references View on Trac View on GitHub

      Related
      Used byDescription
      wp_render_widget()wp-includes/widgets.php

      Calls the render callback of a widget and returns the output.

      dynamic_sidebar()wp-includes/widgets.php

      Displays dynamic sidebar.

      Changelog
      VersionDescription
      2.5.0Introduced.
      User Contributed Notes
      1. Skip to note 2 content Razon Komar Pal 6 years ago You must log in to vote on the helpfulness of this noteVote results for this note: 1You must log in to vote on the helpfulness of this note

        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;
            }
        }
        Log in to add feedback

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

Параметры

$params array обязательный
  • args array An array of widget display arguments.
    • name stringName of the sidebar the widget is assigned to.
    • id stringID of the sidebar the widget is assigned to.
    • description stringThe sidebar description.
    • class stringCSS class applied to the sidebar container.
    • before_widget stringHTML markup to prepend to each widget in the sidebar.
    • after_widget stringHTML markup to append to each widget in the sidebar.
    • before_title stringHTML markup to prepend to the widget title when displayed.
    • after_title stringHTML markup to append to the widget title when displayed.
    • widget_id stringID of the widget.
    • widget_name stringName of the widget.
    • widget_args array An array of multi-widget arguments.
      • number intNumber increment used for multiples of the same widget.
      Source
      $params = apply_filters( 'dynamic_sidebar_params', $params );
      

      View all references View on Trac View on GitHub

      Related
      Used byDescription
      wp_render_widget()wp-includes/widgets.php

      Calls the render callback of a widget and returns the output.

      dynamic_sidebar()wp-includes/widgets.php

      Displays dynamic sidebar.

      Changelog
      VersionDescription
      2.5.0Introduced.
      User Contributed Notes
      1. Skip to note 2 content Razon Komar Pal 6 years ago You must log in to vote on the helpfulness of this noteVote results for this note: 1You must log in to vote on the helpfulness of this note

        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;
            }
        }
        Log in to add feedback

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

Исходный код

wp-includes/widgets.php:813

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

История изменений

ВерсияОписание
2.5.0 Introduced.

Что будем искать? Например,Продвижение

Этот сайт использует куки-файлы. Оставаясь на сайте, Вы соглашаетесь на их использование. Для получения дополнительной информации, пожалуйста, ознакомьтесь с политикой в отношении персональных данных.