• Hi,

    I’m trying to save mutliple checkboxes as metabox, but only the last selection is saved. :/
    I’m using jQuery multipleSelect and when I’m looking at the value, all values should be there.
    Going crazy over here, any suggenstions?

    Form:

    <p>
    <link href="<?php echo get_stylesheet_directory_uri() ; ?>/css/multiple-select.css" rel="stylesheet"/>
    
    <label for="_meta_box_select_options">V?lj kolumnen att lista: </label>
    <select multiple="multiple" name="_meta[list_options]" id="_meta_box_select_options">
      <?php
      // List has been loaded in meta-functions.php
      $i=0;
      foreach ($list as $item) {
        $i++;
        echo('<option value="' . $i . '">' . $item . '</option>');
      } ?>
    </select>
    
    <script src="<?php echo get_stylesheet_directory_uri() ; ?>/js/dev/jquery.multiple.select.js"></script>
    <script>
            var $ =jQuery.noConflict();
            $(function() {
              $("#_meta_box_select_options").multipleSelect({
                  selectAll: false,
                  multiple: true,
                  multipleWidth: 250,
                  width: '100%',
                  onClose: function() {
                    var k = $("#_meta_box_select_options").multipleSelect("getSelects");
                    var text = k.toString();
                   $("#_meta_box_select_options").val(text);
                  }
              });
            });
    </script>
    </p>

    Functions:
    global $post;

    $meta = get_post_meta($post->ID,’_meta’,TRUE);

    // Get all pages available
    $args = array(
    ‘sort_order’ => ‘ASC’,
    ‘sort_column’ => ‘post_title’,
    ‘post_type’ => ‘page’,
    ‘child_of’ => 0,
    ‘parent’ => -1,
    ‘post_status’ => ‘publish’
    );

    $pages = get_pages($args);

    // Set previous selection
    $selected = $meta[‘list_select’];

    // Fetch the HTML
    include(THEME_FOLDER . ‘/UI/meta-form.php’);

    echo ‘<input type=”hidden” name=”_meta_noncename” value=”‘ . wp_create_nonce(__FILE__) . ‘” />’;

    Save:

    if (!wp_verify_nonce($_POST['_meta_noncename'],__FILE__)) return $post_id;
    
        // check user permissions
        if ($_POST['post_type'] == 'page')
        {
            if (!current_user_can('edit_page', $post_id)) return $post_id;
        }
        else
        {
            if (!current_user_can('edit_post', $post_id)) return $post_id;
        }
    
        // authentication passed, save data
    
        $current_data = get_post_meta($post_id, '_meta', TRUE);
        $new_data = $_POST['_meta'];
    
        meta_clean($new_data);
    
        if ($current_data)
        {
            if (is_null($new_data)) delete_post_meta($post_id,'_meta');
            else update_post_meta($post_id,'_meta',$new_data);
        }
        elseif (!is_null($new_data))
        {
            add_post_meta($post_id,'_meta',$new_data,TRUE);
        }

    BR,
    NinjaRat

  • The topic ‘Save multiple checkboxes with metabox.’ is closed to new replies.