I'm wondering if someone could help me. I'm trying to save the value of only the checked checkboxes in a wordpress metabox to the database as an array. So far, I've been able to save each of them as individual columns but I'd like to compile them into only one array, and when the checkbox is unclick, I'd like to update_post_meta to remove just the unclicked one. For some reason, it all keeps getting overridden.
The code I'm using that keeps saving them individually:
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');
The metabox callback that displays the checkboxes:
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
}
My question: How do I save the checked values in one array as opposed to one for each checked value?
Aucun commentaire:
Enregistrer un commentaire