Exciting News! Flipper Code is now WePlugins! Same commitment to excellence, brand new identity.

How to use in_widget_form action in WordPress

Sandeep Kumar Mishra
Sandeep Kumar Mishra
May 12, 2023
5 minutes read

Thinking about adding some extra fields to your widget forms in WordPress? Well, the in_widget_form action hook is just the tool you need. This hook is triggered when the value passed to the ‘widget_form_callback’ isn’t false. You can integrate it into your theme’s functions.php file or even better, create a custom WordPress plugin to ensure your modifications remain intact during theme updates. At WePlugins, we always recommend the plugin route for this reason.

Example 1: Displaying a Custom Field

This example shows how to append a custom field to the end of a widget’s control form.

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

Example 2: Implementing Custom Functionality

Here’s how you can execute a function when the in_widget_form action occurs in WordPress.

    function weplugins_execute_on_in_widget_form_event($widget, $return, $instance){
        // Implement the required additional custom functionality here.
    }
    // Add the action
    add_action( "in_widget_form", "weplugins_execute_on_in_widget_form_event" , 10, 3);
    

Example 3: Removing a Hook Callback

If there’s a need to remove a registered hook, use the remove_action function as shown below.

    remove_action( "in_widget_form", "weplugins_execute_on_in_widget_form_event", 10, 3 );
    

Ensure you provide the same callback function name, priority, and number of arguments when removing the hook callback.

Access Premium WordPress Plugins

Contact Us

If you require any customization or face any issues using this hook, feel free to contact us. We’re here to help!

Sandeep Kumar Mishra

Sandeep Kumar Mishra

Sandeep Kumar Mishra writes about WordPress and Artificial Intelligence, offering tips and guides to help you master your website and stay updated with the latest tech trends.

Explore the latest in WordPress

Trying to stay on top of it all? Get the best tools, resources and inspiration sent to your inbox every Wednesday.