I know there are many questions about variants of this, but I have not been able to find an answer.
I have a metabox on the post type page
, containing nothing but a checkbox. It seems like it won't save no matter what I do. Here is all the code for the metabox.
/*--------------------------------------------------------------------------*
* Register metabox
/*--------------------------------------------------------------------------*/
function kasparabi_page_left_menu() {
add_meta_box( 'kasparabi-left-menu-meta', __( 'Left Menu', 'kasparabi' ), 'kasparabi_render_left_menu_meta_box', 'page', 'side' );
}
add_action( 'add_meta_boxes', 'kasparabi_page_left_menu' );
/*--------------------------------------------------------------------------*
* Callbacks
/*--------------------------------------------------------------------------*/
function kasparabi_render_left_menu_meta_box($post) {
wp_nonce_field( basename( __FILE__ ), 'kasparabi-left-menu-meta_nonce' );
?>
<p>
<div>
<label for="left-menu-checkbox">
<input type="checkbox" name="left-menu-checkbox" <?php (get_post_meta( $post->ID, 'left_menu_checkbox', true) == 'on') ? ' checked="checked"' : ''; ?> />
<?php _e( 'Display left menu', 'kasparabi' )?>
</label>
</div>
</p>
<?php
}
/*--------------------------------------------------------------------------*
* Save functions
/*--------------------------------------------------------------------------*/
function kasparibi_left_menu_meta_save( $post_id, $post ) {
// Checks save status
$is_autosave = wp_is_post_autosave( $post_id );
$is_revision = wp_is_post_revision( $post_id );
$is_valid_nonce = ( isset( $_POST[ 'kasparabi-left-menu-meta_nonce' ] ) && wp_verify_nonce( $_POST[ 'kasparabi-left-menu-meta_nonce' ], basename( __FILE__ ) ) ) ? 'true' : 'false';
// Exits script depending on save status
if ( $is_autosave || $is_revision || !$is_valid_nonce ) {
return $post_id;
}
/* Get the post type object. */
$post_type = get_post_type_object( $post->post_type );
/* Check if the current user has permission to edit the post. */
if ( !current_user_can( $post_type->cap->edit_post, $post_id ) )
return $post_id;
/* Get the posted data and sanitize it for use as an HTML class. */
$new_meta_value = ( isset( $_POST['left-menu-checkbox'] ) ? sanitize_html_class( $_POST['left-menu-checkbox'] ) : '' );
$meta_key = 'left_menu_checkbox';
$meta_value = get_post_meta( $post->ID, $meta_key, true);
/* If a new meta value was added and there was no previous value, add it. */
if ( $new_meta_value && '' == $meta_value )
add_post_meta( $post_id, $meta_key, $new_meta_value, true);
/* If the new meta value does not match the old value, update it. */
elseif ( $new_meta_value && $new_meta_value != $meta_value )
update_post_meta( $post_id, $meta_key, $new_meta_value );
/* If there is no new meta value but an old value exists, delete it. */
elseif ( '' == $new_meta_value && $meta_value )
delete_post_meta( $post_id, $meta_key, $meta_value );
}
add_action( 'save_post', 'kasparibi_left_menu_meta_save' );
wp_postmeta
table right? It is not there.meta_key
asleft_menu_checkbox
and value ason
.