WordPress.org

Ready to get started?Download WordPress

Forums

Widget not saving radio button value (3 posts)

  1. David Gard
    Member
    Posted 3 years ago #

    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
        }
  2. Ryan
    Member
    Posted 3 years ago #

    Hi duck__boy, did you ever figure out what the problem was? I'm faced with the same issue.

  3. Ryan
    Member
    Posted 3 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.