Adding a Checkbox in WordPress Page Settings – Doesn't Save Status
-
Hi All,
Hopefully I’ll be able to explain this well – if not just let me know.
I’m adding some checkboxes to the page settings below the content editor. I already added some fields that work well – I can call them with get_post_meta and add that value into my page template. (image)
However, with these checkboxes, the value of whether it is checked or not does not save when I press the update button.I copied a template that came with the theme (Higher) to figure out how to add the settings boxes in the first place. The important lines of code can be found below. Let me know if I need to upload the whole file.
Array defining area with checkboxes
$meta_boat_page_checks = array( 'id' => 'meta-boat-page-checks', 'title' => __('Boat Amenities', 'mega'), 'page' => 'page', 'context' => 'normal', 'priority' => 'default', 'fields' => array( array( "name" => __('Kitchen', 'mega'), "desc" => __(''), "id" => $prefix."boat_page_check_kitchen", "type" => 'checkbox', "std" => '' ), array( "name" => __('Refrigerator', 'mega'), "desc" => __(''), "id" => $prefix."boat_page_check_refrigerator", "type" => 'checkbox', "std" => '' ) ) );
Add this to the ‘edit page’ page
add_action('admin_menu', 'mega_add_box_page'); function mega_add_box_page() { global $meta_boat_page_checks; add_meta_box($meta_boat_page_checks['id'], $meta_boat_page_checks['title'], 'mega_show_boat_page_checks', $meta_boat_page_checks['page'], $meta_boat_page_checks['context'], $meta_boat_page_checks['priority']); } function mega_show_boat_page_checks() { global $meta_boat_page_checks, $post; // Use nonce for verification echo '<input type="hidden" name="mega_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />'; echo '<table class="form-table mega-custom-table">'; foreach ( $meta_boat_page_checks['fields'] as $field ) { // get current post meta data if ( isset ( $field['id'] ) ) $meta = get_post_meta( $post->ID, $field['id'], true ); switch ( $field['type'] ) { case 'checkbox': echo '<tr>', '<th style="width:25%"><label for="', $field['id'], '"><strong>', $field['name'], '</label></th>', '<td>'; echo '<input name="', $field['id'], '" id="', $field['id'], '" type="checkbox">'; echo '</td>', '</tr>'; break; } } echo '</table>'; }
Inside the function to save the post
foreach ( $meta_boat_page_checks['fields'] as $field ) { if ( isset( $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); } }
I’m not really sure what I need to fix here but I’m not amazing at PHP so my understanding of the code is limited. Could anyone let me know what I need to do to get the checkbox to save its value so I can call that later in a page template?
If you need more information please let me know, thanks!
- The topic ‘Adding a Checkbox in WordPress Page Settings – Doesn't Save Status’ is closed to new replies.