0

So I followed a tutorial on how to create a meta box w/ the taxonomy terms shown in radio buttons. (Tutorial here for others if interested: http://wp.tutsplus.com/tutorials/creative-coding/how-to-use-radio-buttons-with-taxonomies/)

My main goal is to create it to be a drop down. However, when converting the input radio styles to select option, the selected option does not save. Here is the code comparing the two:

      <div id="taxonomy-<?php echo $taxonomy; ?>" class="categorydiv">
          <!-- Display taxonomy terms -->
            <div id="<?php echo $taxonomy; ?>checklist" class="list:<?php echo $taxonomy?> categorychecklist form-no-clear">

              <!-- Display taxonomy terms -->
                <select id="select-taxonomy">
                    <?php foreach($terms as $term){ 
                      $id = $taxonomy.'-'.$term->term_id;
                        $value= (is_taxonomy_hierarchical($taxonomy) ? "value='{$term->term_id}'" : "value='{$term->term_slug}'");
                    echo "<option id='in-$id' name='{$name}'".selected($current,$term->term_id,false)." {$value} />$term->name</option>";
                  } ?>
                </select>
              <!-- Display taxonomy terms -->
                <div id="radio-taxonomy">
                <?php foreach($terms as $term){ 
                  $id = $taxonomy.'-'.$term->term_id;
                    $value= (is_taxonomy_hierarchical($taxonomy) ? "value='{$term->term_id}'" : "value='{$term->term_slug}'");
        echo "<label class='selectit'>";
        echo "<input type='radio' id='in-$id' name='{$name}'".checked($current,$term->term_id,false)." {$value} />$term->name<br />";
                      echo "</label>";
              } ?>
              </div>

            </div>
        </div>

The only thing I can think of that is causing the selection not to save is change the input radio to an element, or it may be changing ".checked" to ".selected"

Any help would be appreciated.

Thanks, Roc.

  • You say the data does not save but you are talking, mostly, about the display-- the checked function. "Saving" the data, and "displaying" the saved data are different things. Have you confirmed that the data does not save to the database? – s_ha_dum May 19 '13 at 14:58
  • No the data is not being saved. If the input is saved via the radio button, both values in the drop down and input radio are selected/displayed on page refresh. However, if when the radio is removed and the value is selected via drop down, the data is not changed upon refresh. This is also confirmed on the taxonomy term wp-list-table, where the post is not changed to the new selected term. – Roc May 19 '13 at 16:21
  • Nothing you posted involves the actual saving of data. The script/tutorial mentioned uses Javascript to do that so you should have a corresponding mechanism. If the data is not saving, that is where the problem is. edit your question to include that code. – s_ha_dum May 19 '13 at 16:37
  • I removed the js and the saving option for the input radio still works, so I concluded that may not be the problem. The javascript clears the selected input on the second tab. I will take a look at it again though. – Roc May 19 '13 at 16:49
  • Ok, now the saving works? Or do you mean that the select menu still shows up? Do you have some other mechanism for saving the data? Something hooked to save_post maybe (which the tutorial mentions but does not pursue)? – s_ha_dum May 19 '13 at 16:56
2

It was a long way around to get there but you basically have a markup error. You didn't name your select. You should have:

<select id="select-taxonomy" name="<?php echo $name ?>"> // <-- here is the change
  <?php foreach($terms as $term) {
    $id = $taxonomy.'-'.$term->term_id;
    $value= (is_taxonomy_hierarchical($taxonomy) ? "value='{$term->term_id}'" : "value='{$term->term_slug}'");
      echo "<option id='in-$id' ". selected($current,$term->term_id,false) ." {$value} />$term->name</option>";
    } ?>
</select>

You don't name the individual options. You name the select itself. With that change, it works just fine.

| improve this answer | |
  • WOW - YOU A GREAT! So, it was just that minimal extra attribute to the <select>? How did you recognize that, that was the problem? I was killing myself since 5 AM last night, and then woke up at 10 to come back at it. – Roc May 19 '13 at 18:36
  • I looked at var_dump($_POST) and couldn't find what I expected, so I went back and worked out why. – s_ha_dum May 19 '13 at 19:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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