Metabox cu checkbox nu se actualizează - Soluții WordPress
15 nov. 2011, 23:15:14
Vizualizări: 18.8K
Voturi: 11
Încerc să configurez un metabox cu un singur checkbox, totul funcționează bine, dar când debifez și salvez postarea, acesta se bifează din nou. Am verificat codul dar nu îmi găsesc greșeala.
Uită-te la codul meu:
function am_checkbox_option() {
global $post;
$custom = get_post_custom($post->ID);
$front_event = $custom["front_event"][0];
wp_nonce_field(__FILE__, 'am_front_event');
if ( $front_event ) {
$checked = "checked=\"checked\"";
} else {
$checked = "";
}
?>
<label>Afișează conținut? (scrie da):</label>
<input type="checkbox" name="front_event" value="true" <?php echo $checked; ?> />
<?php
}
}
add_action('save_post', function() {
if ( defined( 'DOING_AUTOSAVE') && DOING_AUTOSAVE ) return;
global $post;
if ( $_POST && !wp_verify_nonce($_POST['am_front_event'], __FILE__) ) {
return;
}
if ( isset($_POST['front_event']) ) {
update_post_meta($post->ID, 'front_event', $_POST['front_event']);
}
});
Mulțumesc anticipat

andresmijares
1.05K
Toate răspunsurile la întrebare
2
1
Iată codul pe care l-am folosit anterior - principala diferență mi se pare că tu verifici dacă meta există, mai degrabă decât care este valoarea acesteia pentru a determina dacă ar trebui bifată.
// Meta pentru checkbox
add_action("admin_init", "checkbox_init");
function checkbox_init(){
add_meta_box("checkbox", "Checkbox", "checkbox", "post", "normal", "high");
}
function checkbox(){
global $post;
$custom = get_post_custom($post->ID);
$field_id = $custom["field_id"][0];
?>
<label>Bifează pentru da</label>
<?php $field_id_value = get_post_meta($post->ID, 'field_id', true);
if($field_id_value == "yes") $field_id_checked = 'checked="checked"'; ?>
<input type="checkbox" name="field_id" value="yes" <?php echo $field_id_checked; ?> />
<?php
}
// Salvează detaliile Meta
add_action('save_post', 'save_details');
function save_details(){
global $post;
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return $post->ID;
}
update_post_meta($post->ID, "field_id", $_POST["field_id"]);
}

5t3ph
900
16 nov. 2011 00:28:35
Comentarii
1
pur și simplu adaugă o clauză else pentru a șterge metadatele postării dacă nu sunt bifate și codul tău va funcționa perfect, așa că schimbă:
if ( isset($_POST['front_event']) ) {
update_post_meta($post->ID, 'front_event', $_POST['front_event']);
}
în
if ( isset($_POST['front_event']) ) {
update_post_meta($post->ID, 'front_event', $_POST['front_event']);
}else{
delete_post_meta($post->ID, 'front_event');
}

Bainternet
67.7K
16 nov. 2011 00:50:16
Întrebări similare
2
răspunsuri