php - WordPress custom meta boxes not saving for pages -
for reason when save page, meta box information isn't saving. works fine posts though. had working on localhost, after putting online seems have somehow broken it.
function framework_add_meta_box() { $screens = array( 'post', 'page' ); foreach( $screens $screen ) { add_meta_box( 'framework_pagebgimage', __( 'background options', 'framework' ), 'framework_bg_image', $screen, 'normal', 'high' ); } } add_action( 'add_meta_boxes', 'framework_add_meta_box' ); function framework_bg_image( $post ) { global $post; // add nonce field can check later. wp_nonce_field( 'framework_meta_box', 'framework_meta_box_nonce' ); $style = get_post_meta( $post->id, 'bgimage_style', true ); if( !$style ) { $style = 'background'; } /* featured image style */ echo '<label for="bgimage_style">'; _e( 'featured image style:', 'framework' ); echo '</label><br />'; echo '<select id="bgimage_style" name="bgimage_style">'; echo '<option value="background" ' . selected( $style, 'background', false ) . '>background image</option>'; echo '<option value="standard" ' . selected( $style, 'standard', false ) . '>standard</option>'; echo '</select><br /><br />'; } function framework_save_meta_box_data( $post_id ) { // check if our nonce set. if ( ! isset( $_post['framework_meta_box_nonce'] ) ) { return; } // verify nonce valid. if ( ! wp_verify_nonce( $_post['framework_meta_box_nonce'], 'framework_meta_box' ) ) { return; } // if autosave, our form has not been submitted, don't want anything. if ( defined( 'doing_autosave' ) && doing_autosave ) { return; } // check 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; } } if ( ! isset( $_post['bgimage_style'] ) ) { return; } $style = sanitize_text_field( $_post['bgimage_style'] ); update_post_meta( $post_id, 'bgimage_style', $style ); } add_action( 'save_post', 'framework_save_meta_box_data' );
verify $_post['post_type']
value.
also, instead of use $_post
value use second parameter in function, this:
function framework_save_meta_box_data( $post_id, $post ) { // check if our nonce set. if ( ! isset( $_post['framework_meta_box_nonce'] ) ) { return; } // verify nonce valid. if ( ! wp_verify_nonce( $_post['framework_meta_box_nonce'], 'framework_meta_box' ) ) { return; } // if autosave, our form has not been submitted, don't want anything. if ( defined( 'doing_autosave' ) && doing_autosave ) { return; } // check user's permissions. if ( 'page' == $post->post_type ) { if ( ! current_user_can( 'edit_page', $post_id ) ) { return; } } else { if ( ! current_user_can( 'edit_post', $post_id ) ) { return; } } if ( ! isset( $_post['bgimage_style'] ) ) { return; } $style = sanitize_text_field( $_post['bgimage_style'] ); update_post_meta( $post_id, 'bgimage_style', $style ); } add_action( 'save_post', 'framework_save_meta_box_data', 10, 2 );