WordPress.org

Support

Support » How-To and Troubleshooting » Widget not saving radio button value

Widget not saving radio button value

  • Hey all,

    I have multi-widget that allows user to select a style for the code, but I cannot get the widget to save to value of the selected radio button. I’m wondering if anybody can spot an error in my code at all?

    Thanks.

    /** @see WP_Widget::update */
        function update($new_instance, $old_instance){
    	$instance = $old_instance;
    	$instance['title'] = strip_tags($new_instance['title']);
    	$instance['testimonials_style'] = strip_tags($new_instance['testimonials_style']);
            return $instance;
        }
    
        /** @see WP_Widget::form */
        function form($instance){
    		$title = esc_attr($instance['title']);
    		$testimonials_style = esc_attr($instance['testimonials_style']);
            ?>
    			<p>
    				<label for="<?php echo $this->get_field_id('title'); ?>">
    
    					<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />
    				</label>
    			</p>
    			<?php _e('Use the following style:'); ?>
    			<p>
    				<label for="<?php echo $this->get_field_id('style_sidebar'); ?>">
    					<?php _e('Sidebar:'.$testimonials_style); ?>
    					<input class="" id="<?php echo $this->get_field_id('style_sidebar'); ?>" name="<?php echo $this->get_field_id('testimonials_style'); ?>" type="radio" value="style_sidebar" <?php if($testimonials_style === 'style_sidebar'){ echo 'checked="checked"'; } ?> />
    				</label>
    				<label for="<?php echo $this->get_field_id('style_box'); ?>">
    					<?php _e('Box:'.$testimonials_style); ?>
    					<input class="" id="<?php echo $this->get_field_id('style_box'); ?>" name="<?php echo $this->get_field_id('testimonials_style'); ?>" type="radio" value="style_box" <?php if($style_sidebar === 'style_box'){ echo 'checked="checked"'; } ?> />
    				</label>
    			</p>
            <?php
        }
Viewing 2 replies - 1 through 2 (of 2 total)
  • Hi duck__boy, did you ever figure out what the problem was? I’m faced with the same issue.

    Doh, found my problem (and yours as well). For setting the name=”” value of the radio we were both using:

    $this->get_field_id(‘foobar’);

    Instead of:

    $this->get_field_name(‘foobar’);

    It’s always something obvious and easy to miss, eh?

    Hope this saves someone else the wasted time it cost me.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Widget not saving radio button value’ is closed to new replies.
Skip to toolbar