Radio buttons in widget not saving

Got it, it turns out…

  1. The label for each radio button must be the field id / variable, in my example is ‘radio_buttons’
  2. The IF statement for each radio button must refer to this same ID – $radio_buttons === ‘radio_option_1’ & $radio_buttons === ‘radio_option_2’

My new code which works is…

<p>
    <label for="<?php echo $this->get_field_id('text_area'); ?>">
        <?php echo('Radio buttons'); ?>
    </label><br>
    <label for="<?php echo $this->get_field_id('radio_buttons'); ?>">
        <?php _e('Option 1:'); ?>
        <input class="" id="<?php echo $this->get_field_id('radio_option_1'); ?>" name="<?php echo $this->get_field_name('radio_buttons'); ?>" type="radio" value="radio_option_1" <?php if($radio_buttons === 'radio_option_1'){ echo 'checked="checked"'; } ?> />
    </label><br>
    <label for="<?php echo $this->get_field_id('radio_buttons'); ?>">
        <?php _e('Option 2:'); ?>
        <input class="" id="<?php echo $this->get_field_id('radio_option_2'); ?>" name="<?php echo $this->get_field_name('radio_buttons'); ?>" type="radio" value="radio_option_2" <?php if($radio_buttons === 'radio_option_2'){ echo 'checked="checked"'; } ?> />
    </label>
    </p>