Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Logically, I'm thinking it should something like...

<input type="radio" id="my_category_layout">
    <option value="Item1">Item 1</option>
    <option value="Item2">Item 2</option>
    <option value="Item3">Item 3</option>
</input>

But from what i've seen radio buttons dont markup like that.

My existing code is below that creates a select list pulldown menu that allows me to capture a selected value. I'm struggling with how to convert this into a radio group.

The main issue is that there is only one ID to represent the select, but I can't figure how to create three radio buttons with a single ID value that can be passed to represent the selected button.

How would I do that? Here's my existing code for creating a select with a single id and multiple values...

<?php
$mySelectOptions = array("list" => "List View", "grid1" => "Grid View Multi Column", "grid2" => "Grid View Two Column");

array(  "name" => "Category Layout",
     "desc" => "description goes here",
     "id" => "my_category_layout",
     "type" => "select",
    "options" => $mySelectOptions ),
?>
<form method="post">

<?php foreach ($options as $value) 
{
    switch ( $value['type'] ) 
    {
    case "select":
    ?>
    <select class="input" name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>">
    <?php foreach ($value['options'] as $option) { ?>
        <option<?php if ( get_settings( $value['id'] ) == $option) { echo ' selected="selected"';
        } 
        elseif ($option == $value['std']) 
        { 
        echo ' selected="selected"'; 
        } ?> value='<?php echo $option; ?>'>
        <?php echo $option; ?></option><?php } ?>
    </select>
<?php
break;
case "radio":
?>
share|improve this question
    
I've updated your question title, i found the original a little misleading/confusing when considering the actual question, hope you don't mind.. ;) –  t31os Jan 23 '11 at 10:15
    
Are you writing the switch? Are you seeking help in creating the radio case inside the switch? If the switch case is already written, i'd have thought switching that option's type to radio would be the quick/easy solution, no? –  t31os Jan 23 '11 at 10:20
add comment

1 Answer

up vote 1 down vote accepted
case "radio":
  foreach ($value['options'] as $option) { ?>
     <label for="<?php echo "{$value['id']}_{$option}"; ?>">
     <input
        type="radio"
        name="<?php echo $value['id']; ?>"
        id=<?php echo "{$value['id']}_{$option}"; ?>
        value="<?php echo $option; ?>"
        <?php checked($option, get_settings($value['id'])); ?>
     />
     <?php echo $option; ?>
     </label>
  <?php }
break;

But I don't recommend coding theme/plugin settings this way.

The array above doesn't just represent the option:value, but also the type of input, possible values (description, id?)... This just feels wrong. And you're making the code lack flexibility.

What if you decide later to change a select field with a radio field? Or what if you want to add a image describing the option, or a color picker?

Why do you think WP's core options are not coded this way?

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.