What’s a good way to reduce or simplify the following php function to check the applicable input field?
<?php
$options = get_option( 'navbar_style', '' );
$right = $left = $fade = '';
if( isset( $options['style'] ) ) {
if( $options['style'] == 'right' ) {
$right = ' checked="checked"';
}
if( $options['style'] == 'left' ) {
$left = ' checked="checked"';
}
if( $options['style'] == 'fade' ) {
$fade = ' checked="checked"';
}
}
?>
<label><input type='radio' name='navbar_style[style]' value='right' <?php echo $right; ?> >Right <em>(Default)</em></label>
<label><input type='radio' name='navbar_style[style]' value='left' <?php echo $left; ?> >Left</label>
<label><input type='radio' name='navbar_style[style]' value='fade' <?php echo $fade; ?> >Fade</label>
4
Answers
You can use dynamic variable setting, but not recommended! See next example for proper use.
A much cleaner way would be this version and use for example
$checks['left']
instead of$left
.You can try this:
You may also do it like this:
Perhaps more compact to create a function and pass in the options, then it’s just a test to see if that option is set and return the corresponding html…