Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Problem: I couldn't overload the update() method of an existing widget. The widget method and the form method work fine but I can't update the value of the new options defined in the form method.

Bellow is how I made that:

I want to use the default WP widgets from my custom theme and I need to give them the look and feel of my company.

I inherited the WP_Widget_Pages class and overloaded the form() (I need to add two options), the update (to manage the new options) and the widget() methods.

In my functions.php it look like this :

function load_my_widgets() {
register_widget('WP_MY_Widget_Pages');
}

/* Add our function to the widgets_init hook. */
add_action( 'widgets_init', 'load_my_widgets' );

class WP_MY_Widget_Pages extends WP_Widget_Pages {

function widget( $args, $instance ) {
    parent::widget($args, $instance)
}

function update( $new_instance, $old_instance ) {
    $first_instance = parent::update($new_instance, $old_instance);
    $first_instance['block_type'] = 'my_block_type';
    $first_instance['block_color'] = 'my_block_color';

    return $first_instance;
}

function form( $instance ) {
    //Defaults
    $instance = wp_parse_args( (array) $instance, array( 'sortby' => 'post_title', 'title' => '', 'exclude' => '', 'block_type' => '', 'block_color' => '' ));
    $block_type = esc_attr( $instance['block_type'] );
    $block_color = esc_attr( $instance['block_color'] );
    parent::form($instance);
    ?>
        <p>
            <label for="<?php echo $this->get_field_id('block_type'); ?>"><?php _e( 'Block type:' ); ?></label>
            <select name="<?php echo $this->get_field_name('block_type'); ?>" id="<?php echo $this->get_field_id('block_type'); ?>" class="widefat">
                <?php foreach (get_all_block_type() as $block_label => $block_type): ?>
                    <option <?php selected( $block_type , $instance['block_type'] ); ?> value="<?php echo $block_type; ?>"><?php _e($block_label); ?></option>
                <?php endforeach; ?>
            </select>
            <?php echo 'Block type : '.$instance['block_type']; ?>
        </p>
        <p>
            <label for="<?php echo $this->get_field_id('block_color'); ?>"><?php _e( 'Block color:' ); ?></label>
            <select name="<?php echo $this->get_field_name('block_color'); ?>" 
                    id="<?php echo $this->get_field_id('block_color'); ?>" 
                    class="widefat" style="background-color: <?php echo $block_color_value; ?>" >
                <?php foreach (get_all_block_color() as $block_color_label => $block_color_class): ?>
                    <option <?php selected( $block_color_label , $instance['block_color'] ); ?> value="<?php echo $block_color_label;?>" style="background-color: <?php echo $block_color_class;?>" >&nbsp;</option>
                <?php endforeach; ?>
            </select>
            <?php echo 'block color : '.$instance['block_color']; ?>
        </p>

    <?php
}

}

I have cleaned up the widget method for reading purpose. I did not bother you with validation in the update(), I have set a literal value to make it short.

When I go to the dashboard/widget page, then display my Page widget, I can set values to the two new attributes. When I save, the output of the new option's value shows nothing (there is nothing too in the database: select option_value from wp_options where option_id='106';)

I would really appreciate any hint.

Thanks in advance.

share|improve this question
add comment

1 Answer 1

up vote 0 down vote accepted

My colleague ran into a similar issue a while back. He never did manage to track down the WordPress bug but doing unregister_widget( 'Widget_Class_Being_Extended' ) did the trick.

share|improve this answer
    
Simply awesome. Thank you very much (to your colleague too). –  Lilian Sep 14 '11 at 11:47
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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