If you want to save a checkbox meta box in WordPress? Use following code in your theme’s functions.php file OR in site specific plugin.
For creating a checkbox, use the below code:
1 2 3 4 5 6 7 8 9 10 11 | <?php function sl_meta_box_sidebar(){ global $post; $custom = get_post_custom($post->ID); $sl_meta_box_sidebar = $custom["sl-meta-box-sidebar"][0]; ?> <input type="checkbox" name="sl-meta-box-sidebar" <?php if( $sl_meta_box_sidebar == true ) { ?>checked="checked"<?php } ?> /> Check the Box. <?php } ?> |
Now use the below code to save the checkbox value:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <?php add_action('save_post', 'save_details'); function save_details($post_ID = 0) { $post_ID = (int) $post_ID; $post_type = get_post_type( $post_ID ); $post_status = get_post_status( $post_ID ); if ($post_type) { update_post_meta($post_ID, "sl-meta-box-sidebar", $_POST["sl-meta-box-sidebar"]); } return $post_ID; } ?> |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.