Multi-select field for Taxonomy can’t save the value

I’m trying to create a multi select field for Taxonomy pages,
and the problem is in saving all the selected values, for example if I choose couple of elements in the select field and save it, it will save only value of the last selected element and ignore the others, how can I save all selected values?

function 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) 

Related posts

Leave a Reply