2017-06-23 5 views
0

Ich frage mich, ob jemand mir helfen könnte. Ich versuche, den Wert von nur die checked Checkboxen in einem Wordpress-Metabox auf die Datenbank als ein Array zu speichern. Bis jetzt war ich in der Lage, jede von ihnen als einzelne Spalten zu speichern, aber ich möchte sie in nur einem Array zusammenstellen, und wenn das Kontrollkästchen deaktiviert ist, möchte ich update_post_meta nur den unclicked entfernen. Aus irgendeinem Grund wird alles immer wieder außer Kraft gesetzt.Speichern checked checkbox Werte als Array

Der Code verwende ich, dass sie hält individuell zu speichern:

function save_location_meta($post_id) { 
    global $location_meta_fields; 
    // verify nonce 
    if (!isset($_POST['location_meta_box_nonce']) || !wp_verify_nonce($_POST['location_meta_box_nonce'], basename(__FILE__))) 
     return $post_id; 
    // check autosave 
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) 
     return $post_id; 
    // check permissions 
    if ('page' == $_POST['post_type']) { 
     if (!current_user_can('edit_page', $post_id)) 
      return $post_id; 
    } elseif (!current_user_can('edit_post', $post_id)) { 
     return $post_id; 
    } 
    // loop through fields and save the data 
    foreach ($location_meta_fields as $field) { 
     $old = get_post_meta($post_id, $field['id'], true); 
     $new = $_POST[$field['id']]; 
     if($new == '' && !$old && array_key_exists('default',$field)){ 
      $new = $field['default']; 
     } 
     if ($new != '' && $new != $old) { 
      update_post_meta($post_id, $field['id'], $new); 

     } elseif ($new == '' && $old != '') { 

      $post_users = get_weekly_stats($post_id, $field['id'], $old); 

      $uid_key = array_search($field['id'], $post_users); 

      unset($post_users[$uid_key]); 

      update_post_meta($post_id, "_test_one", $uid_key); 

      update_post_meta($post_id, "test", $post_users); 



      delete_post_meta($post_id, $field['id'], $old); 

     } 
    } // end foreach 




} 
add_action('save_post', 'save_location_meta'); 

Die metabox Rückruf, der die Kontrollkästchen anzeigt:

function one_callback($post) { 

    global $location_meta_fields, $post; 
    // Use nonce for verification 
    echo '<input type="hidden" name="location_meta_box_nonce" value="'.wp_create_nonce(basename(__FILE__)).'" />'; 
    // Begin the field table and loop 
    echo '<table class="form-table">'; 
    foreach ($location_meta_fields as $field) { 
     // get value of this field if it exists for this post 
     $meta = get_post_meta($post->ID, $field['id'], true); 
     // begin a table row with 
     echo '<tr> 
       <td>'; 
       echo '<input type="checkbox" name="'.$field['id'].'" id="'.$field['id'].'" ',$meta ? ' checked="checked"' : '','/> 
        <label for="'.$field['id'].'">'.$field['label'].'</label>'; 

     echo '</td></tr>'; 
    } // end foreach 
    echo '</table>'; // end table 
} 

Meine Frage: Wie speichere ich die überprüften Werte in einem Array im Gegensatz zu einem für jeden überprüften Wert?

Antwort

0

Sie müssen die Namen der Eingänge mit "[]" schreiben, dann behandelt PHP die gespeicherten Werte als Array.

function one_callback($post) { 

    global $location_meta_fields, $post; 
    // Use nonce for verification 
    echo '<input type="hidden" name="location_meta_box_nonce" value="'.wp_create_nonce(basename(__FILE__)).'" />'; 
    // Begin the field table and loop 
    echo '<table class="form-table">'; 
    foreach ($location_meta_fields as $field) { 
     // get value of this field if it exists for this post 
     $meta = get_post_meta($post->ID, $field['id'], true); ?> 
     <tr> 
      <td> 
       <input type="checkbox" name="fields[]" value="<?php echo $field['id']; ?>" id="<?php echo $field['id']; ?>" <?php checked($meta); ?>/> 
       <label for="<?php echo $field['id']; ?>"><?php echo $field['label']; ?></label>'; 
      </td> 
     </tr> 
    <?php } // end foreach 
    echo '</table>'; // end table 
} 

Also, wenn Sie zum Beispiel wollen den ersten Wert zuzugreifen, die überprüft wurde, können Sie es wie diese $_POST['fields'][0] tun.

Dann in Ihrer Funktion save_location_meta(), Sie müssen nicht über die Felder in einer Schleife, können Sie einfach sie so an die DB als Array speichern: update_post_meta($post_id, 'my-fields', $_POST['fields']);