checkbox issue with wordpress update_post_meta()
-
Hi,
usually it is a PHP issue but in my case i read on internet several posts about the way how to do it and it seems my code is correct, so i do not understand where is the problem.
I’m creating a plugin that should modify posts appearance.
For that i have a meta box where i have a checkbox and i want to store in DB the status of that check box (checked or not).
For that i use the following code:/* * Function to render the meta box added by the plugin in post */ function rgt_metabox_callback( $post ) { // Add an nonce field so we can check for it later. wp_nonce_field( 'rgt_meta_box', 'rgt_meta_box_nonce' ); /* * Use get_post_meta() to retrieve an existing value * from the database and use the value for the form. */ $rgt_post_images_slider = get_post_meta( $post->ID, '_rgt_post_images_slider', true ); $rgt_post_images_slider_usage = get_post_meta( $post->ID, '_rgt_post_images_slider_usage', true ); echo '<label for="rgt_post_thumbnail_slider">'; if($rgt_post_images_slider_usage == "true"){ echo '<input type="checkbox" id="rgt_image_list_usage" name="rgt_image_list_usage" checked="checked" value="true" />'; } else{ echo '<input type="checkbox" id="rgt_image_list_usage" name="rgt_image_list_usage" value="true" />'; } _e( 'Use images slider instead of Featured image.', 'rgt-post-thumbnail-slider-plugin' ); echo '</label> ';
and later:
function rgt_save_meta_box_data( $post_id ) { /* * We need to verify this came from our screen and with proper authorization, * because the save_post action can be triggered at other times. */ // Check if our nonce is set. if (!isset($_POST['rgt_meta_box_nonce'])) { return; } // Verify that the nonce is valid. if (!wp_verify_nonce($_POST['rgt_meta_box_nonce'], 'rgt_meta_box')) { return; } // If this is an autosave, our form has not been submitted, so we don't want to do anything. if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { return; } // Check the user's permissions. if (isset($_POST['post_type']) && 'page' == $_POST['post_type']) { if (!current_user_can('edit_page', $post_id)) { return; } } else { if (!current_user_can('edit_post', $post_id)) { return; } } /* OK, it's safe for us to save the data now. */ // Make sure that it is set. if (!isset($_POST['rgt_image_list'])) { return; } if (!isset($_POST['rgt_image_list_usage'])) { return; } // Sanitize user input. $my_data = sanitize_text_field($_POST['rgt_image_list']); $img_slider_usage = ""; error_log("value of checkbox (in SAVE): ".$_POST["rgt_image_list_usage"]); if (isset($_POST["rgt_image_list_usage"])) { $img_slider_usage = $_POST['rgt_image_list_usage']; } // Update the meta field in the database. update_post_meta( $post_id, '_rgt_post_images_slider', $my_data ); update_post_meta( $post_id, '_rgt_post_images_slider_usage', $img_slider_usage ); } add_action( 'save_post', 'rgt_save_meta_box_data' );
However when checkbox is not checked, it does not clear value in DB.
So where is my mistake ?
thx
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
- The topic ‘checkbox issue with wordpress update_post_meta()’ is closed to new replies.