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?