单选按钮选择值保留

I'm working on a WordPress settings page that has a radio button with 2 given options 'yes' or 'no' but it doesn't seem to retain the chosen option, it deselects after a refresh. It's of great importance that the chosen option stays selected because it triggers the display of an element. I've researched and experimented with the PHP function below but it still doesn't retain the chosen option.

function section_footer() {}
    add_settings_field('socialbar', 'Display Social Bar', 'socialbar', __FILE__, 'footer_settings' );

}
    function socialbar() {
        $options = get_option('theme_options');  echo "<input name='theme_options[socialbar]' type='radio' value='yes'  />"; echo 'Yes';  echo "&nbsp;&nbsp;&nbsp;";
            if (isset($socialbar) && $socialbar=="yes") echo "checked";

        $options = get_option('theme_options');  echo "<input name='theme_options[socialbar]' type='radio' value='no'  />"; echo 'No';
        if (isset($socialbar) && $socialbar=="no") echo "checked";

    }

You need to echo the "checked" inside of the input declaration.

Change your PHP to the following. You can do the if statement inline using a ternary operator.

function socialbar() {
    $options = get_option('theme_options');  echo "<input name='theme_options[socialbar]'   type='radio' value='yes' ".(isset($socialbar) && $socialbar=="yes" ? "checked" : "")." />"; 

    echo 'Yes';  echo "&nbsp;&nbsp;&nbsp;";

    $options = get_option('theme_options');  echo "<input name='theme_options[socialbar]' type='radio' value='no' ".(isset($socialbar) && $socialbar=="no" ? "checked" : "")."  />"; echo 'No';

}