Can’t parse widget radio button

I’m creating wordpress theme with custom widgets, so i want to make a sample widget with 3 radio buttons, so far so good, but the update() function, doesn’t work well here is my code :

function update( $new_instance, $old_instance ) {

$instance = $old_instance;

$instance['default-image'] = strip_tags($new_instance['default-image']);
$instance['post-image'] =  strip_tags($new_instance['post-image']);
$instance['link-image'] =  strip_tags($new_instance['link-image']);

return $instance;

}

Read More
function form( $instance ) {

$defaults = array('default-image' => true, 'post-image' => false, 'link-image' => false );
$instance = wp_parse_args( (array) $instance, $defaults ); 

?>

<p>
    <input name="option" type="radio" <?php checked($instance['default-image']); ?> id="<?php echo $this->get_field_id('default-image'); ?>"/> 
    <label for="<?php echo $this->get_field_id( 'default-image' ); ?>"><?php _e('Default image'); ?></label>
</p>
<p>
    <input name="option" type="radio" <?php checked( $instance['post-image']); ?> id="<?php echo $this->get_field_id('post-image'); ?>" /> 
    <label for="<?php echo $this->get_field_id( 'post-image' ); ?>"><?php _e('First post image'); ?></label>
</p>
<p>
    <input name="option" type="radio" <?php checked( $instance['link-image']); ?> id="<?php echo $this->get_field_id('link-image') ?>" /> 
    <label for="<?php echo $this->get_field_id( 'link-image' ); ?>"><?php _e('Image from link'); ?></label>
</p>

i will provide them in widget() with $instance[‘default-image’], $instance[‘post-image’], $instance[‘link-image’], but for now on “Save” button or (refresh on admin page) selected option disappears . If anyone can tell me where i’m wrong, i will appreciate it.

Related posts

Leave a Reply

2 comments

  1. Answer on this questiton, that i found after several hours of investingating is that there isn’t clear way to do it. Name attribute on “radio” buttons, should be same foreach button, but to work normally (to save and get saved values), name should be given by wordpress via echo ($this->get_field_name(‘default/post/link-image’); i found another solution using numeric values :
    update function ():

    function update( $new_instance, $old_instance ) {
    
        $instance = $old_instance;
    
        $instance['rating'] = ( isset( $new_instance['rating'] ) && $new_instance['rating'] > 0 && $new_instance['rating'] < 4 ) ? (int) $new_instance['rating'] : 0;
    
        return $instance;
    }
    

    and :

    function form( $instance ) {
    
            $instance = wp_parse_args( (array)$instance, $defaults ); 
            //default value
            $rating = ( isset( $instance['rating'] ) && is_numeric( $instance['rating'] ) ) ? (int) $instance['rating'] : 1;
            ?>
                <p>
                <legend>Use image for:</legend>
                    <input type="radio" id="<?php echo ($this->get_field_id( 'rating' ) . '-1') ?>" name="<?php echo ($this->get_field_name( 'rating' )) ?>" value="1" <?php checked( $rating == 1, true) ?>>
                    <label for="<?php echo ($this->get_field_id( 'rating' ) . '-1' ) ?>"><?php _e('Default image') ?></label> <br />
    
                    <input type="radio" id="<?php echo ($this->get_field_id( 'rating' ) . '-2') ?>" name="<?php echo ($this->get_field_name( 'rating' )) ?>" value="2" <?php checked( $rating == 2, true) ?>>
                    <label for="<?php echo ($this->get_field_id( 'rating' ) . '-2' ) ?>"><?php _e('First image post') ?></label> <br />
    
                    <input type="radio" id="<?php echo ($this->get_field_id( 'rating' ) . '-3') ?>" name="<?php echo ($this->get_field_name( 'rating' )) ?>" value="3" <?php checked( $rating == 3, true) ?>>
                    <label for="<?php echo ($this->get_field_id( 'rating' ) . '-3' ) ?>"><?php _e('Image from internet. Note please save first') ?></label> <br />  
                </p>
        <?php
        }