do_action_ref_array( 'in_widget_form', WP_Widget $this, null $return, array $instance )

Fires at the end of the widget control form.


Description Description

Use this hook to add extra fields to the widget form. The hook is only fired if the value passed to the ‘widget_form_callback’ hook is not false.

Note: If the widget has no form, the text echoed from the default form method can be hidden using CSS.


Top ↑

Parameters Parameters

$this

(WP_Widget) The widget instance (passed by reference).

$return

(null) Return null if new fields are added.

$instance

(array) An array of the widget's settings.


Top ↑

Source Source

File: wp-includes/class-wp-widget.php

View on Trac



Top ↑

Changelog Changelog

Changelog
Version Description
2.8.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by Razon

    Add custom widget field end of the 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();
        }
    }

    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;
        }
    }

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