Skip to content

Instantly share code, notes, and snippets.

@LinzardMac
Created February 12, 2015 18:57
Show Gist options
  • Save LinzardMac/461a47865eb1394c310b to your computer and use it in GitHub Desktop.
Save LinzardMac/461a47865eb1394c310b to your computer and use it in GitHub Desktop.
/* the lines of code that generates ALL checkbox fields*/
case 'checkbox':
echo '<input type="checkbox" name="'.$field['id'].'" class="'.$field['id'].'" id="'.$field['id'].'" ',$meta ? ' checked="checked"' : '','/>
<label for="'.$field['id'].'">'.$field['desc'].'</label>';
break;
/* the actual HTML output seems the same */
<tr>
<th><label for="custom_multiday">Multi-Day</label></th>
<td><input type="checkbox" name="custom_multiday" class="custom_multiday" id="custom_multiday" checked="checked">
<label for="custom_multiday"></label></td></tr>
<tr><!-- only this field actually saves the OFF status -->
<th><label for="custom_recurring">Recurring</label></th>
<td><input type="checkbox" name="custom_recurring" class="custom_recurring" id="custom_recurring" checked="checked">
<label for="custom_recurring"></label></td></tr>
<tr>
<th><label for="custom_cost_free">Free Admission? </label></th>
<td><input type="checkbox" name="custom_cost_free" class="custom_cost_free" id="custom_cost_free" checked="checked">
<label for="custom_cost_free">Check box if admission if free</label></td></tr>
/* code for the saving (truncated) */
// loop through fields and save the data
foreach ($custom_meta_fields as $field) {
if(isset($_POST[$field['id']])){
$old = get_post_meta($post_id, $field['id'], true);
$new = $_POST[$field['id']];
if ($new && $new != $old) {
update_post_meta($post_id, $field['id'], $new);
} elseif ('' == $new && $old) {
delete_post_meta($post_id, $field['id'], $old);
}
}elseif(!isset($_POST[$field['id']])){
// this deletes the value if the field has nothing in it.
delete_post_meta($post_id, $field['id'], $old);
}// end if isset
} // end foreach
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment