Adding a checkbox to custom write panels
-
Has anyone successfully added a custom write panel with a checkbox? I’ve been able to figure out how to get the checkbox to save the custom field value when I check it and save a post, but if I want to uncheck it, it doesn’t update. Here is an abridged version of the code:
Here is the HTML for the form:
<input type="checkbox" name="<?php echo $metabox["name"]?>" id="<?php echo $metabox["name"]?>" value="yes"'; <?php if (get_post_meta($post->id, $metabox["name"], true) == "yes" ) { echo ' checked="checked"'; } ?> />
And then the POST function:
function metabox_insert($pID) { global $metaboxes; foreach ($metaboxes as $metabox) { $var = $metabox["name"]; if (isset($_POST[$var])) { if( get_post_meta( $pID, $metabox["name"] ) == "" ) add_post_meta($pID, $metabox["name"], $_POST[$var], true ); elseif($_POST[$var] != get_post_meta($pID, $metabox["name"], true)) update_post_meta($pID, $metabox["name"], $_POST[$var]); elseif($_POST[$var] == "") delete_post_meta($pID, $metabox["name"], get_post_meta($pID, $metabox["name"], true)); } } }
Viewing 7 replies - 1 through 7 (of 7 total)
Viewing 7 replies - 1 through 7 (of 7 total)
- The topic ‘Adding a checkbox to custom write panels’ is closed to new replies.