Multi-select field for Taxonomy
-
Hello, I’m trying to create a multi select field for Taxonomy,
and the problem is that I can pass all the values of the fields in to array, for example if I select couple of items int the select fiels and save it, it will save only value of the last element and ignor the othersfunction books_tax_fields($tag) { $t_id = $tag->term_id; $term_meta = get_option( "taxonomy_$t_id"); print_r($term_meta); ?> <tr class="form-field"> <th scope="row" valign="top"> <label for="Book_Store_Location"> <?php _e('Book Store Location'); ?> </label></th> <td> <input type="text" name="term_meta[book_store_location]" id="term_meta[book_store_location]" size="25" style="width:100%;" value="<?php echo $term_meta['book_store_location'] ? $term_meta['book_store_location'] : ''; ?>"> <br /> <span class="description descRed"> <?php _e('Book Store Location URL'); ?> </span></td> </tr> <tr class="form-field"> <th scope="row" valign="top"> <label for="Books_Select_box"> <?php _e('Select your favorite book'); ?> </label></th> <td> <select name='term_meta[books] ' data-placeholder="Select your favorite books..." class="books-slkt" multiple style="width:100%; height:100px;" tabindex="4"> <option value=""></option> <option value="1">Book one</option> <option value="2">Book two</option> <option value="3">Book three</option> <option value="4">Book four</option> <option value="5">Book five</option> </select> </td> </tr> <?php } function save_extra_books_fields( $term_id ) { if ( isset( $_POST['term_meta'] ) ) { $t_id = $term_id; $term_meta = get_option( "taxonomy_$t_id"); $cat_keys = array_keys($_POST['term_meta']); foreach ($cat_keys as $key){ if (isset($_POST['term_meta'][$key])){ $term_meta[$key] = $_POST['term_meta'][$key]; } } update_option( "taxonomy_$t_id", $term_meta ); } } add_action( 'books_edit_form_fields', 'books_tax_fields', 10, 2); add_action( 'edited_books', 'save_extra_books_fields', 10, 2);And Here is results I get
Array ( [book_store_location] => [books[] => 5)
The topic ‘Multi-select field for Taxonomy’ is closed to new replies.