3

I want to add input box in the custom form when we click for Widget Settings. Really like to have this additional text box in all widgets be Custom Made or the default ones. I'll be needing help in adding new configurable inputbox in Wordpress widget for all widget registered with Theme.

Your inputs/suggestions awaited, Thanks.

2 Answers 2

10

You'd want too look into the following actions and filters:

  • in_widget_form - To add stuff to an existing form.
  • widget_update_callback - To save the content of your new form.
  • widget_display_callback - To add output to the existing output.

With a bit of searching in the forums and on Google you'll find lots of help, but I found this page quite useful to get started myself.

0
7

The link posted by Linda is a gem! Unfortunately, its broken now. Just retrieved the content of that through time back machine:

Step 1: Register the form elements (The widget Control)

function kk_in_widget_form($t,$return,$instance){
    $instance = wp_parse_args( (array) $instance, array( 'title' => '', 'text' => '', 'float' => 'none') );
    if ( !isset($instance['float']) )
        $instance['float'] = null;
    if ( !isset($instance['texttest']) )
        $instance['texttest'] = null;
    ?>
    <p>
        <input id="<?php echo $t->get_field_id('width'); ?>" name="<?php echo $t->get_field_name('width'); ?>" type="checkbox" <?php checked(isset($instance['width']) ? $instance['width'] : 0); ?> />
        <label for="<?php echo $t->get_field_id('width'); ?>"><?php _e('halbe Breite'); ?></label>
    </p>
    <p>
        <label for="<?php echo $t->get_field_id('float'); ?>">Float:</label>
        <select id="<?php echo $t->get_field_id('float'); ?>" name="<?php echo $t->get_field_name('float'); ?>">
            <option <?php selected($instance['float'], 'none');?> value="none">none</option>
            <option <?php selected($instance['float'], 'left');?>value="left">left</option>
            <option <?php selected($instance['float'], 'right');?> value="right">right</option>
        </select>
    </p>
    <input type="text" name="<?php echo $t->get_field_name('texttest'); ?>" id="<?php echo $t->get_field_id('texttest'); ?>" value="<?php echo $instance['texttest'];?>" />
    <?php
    $retrun = null;
    return array($t,$return,$instance);
}

Step 2: Save the Widget input data:

function kk_in_widget_form_update($instance, $new_instance, $old_instance){
    $instance['width'] = isset($new_instance['width']);
    $instance['float'] = $new_instance['float'];
    $instance['texttest'] = strip_tags($new_instance['texttest']);
    return $instance;
}

Step 3: Display the value in widget output:

function kk_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'];
    if (isset($widget_opt[$widget_num]['width'])){
            if(isset($widget_opt[$widget_num]['float']))
                    $float = $widget_opt[$widget_num]['float'];
            else
                $float = '';
            $params[0]['before_widget'] = preg_replace('/class="/', 'class="'.$float.' half ',  $params[0]['before_widget'], 1);
    }
    return $params;
}

Step 4: Now call your functions above with wordpress action hooks:

//Add input fields(priority 5, 3 parameters)
add_action('in_widget_form', 'kk_in_widget_form',5,3);
//Callback function for options update (priorität 5, 3 parameters)
add_filter('widget_update_callback', 'kk_in_widget_form_update',5,3);
//add class names (default priority, one parameter)
add_filter('dynamic_sidebar_params', 'kk_dynamic_sidebar_params');

Source

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.