WordPress自定义元框不保存页面

时间:2015-04-10 00:21:05

标签: php wordpress

出于某种原因,当我保存页面时,元框信息不会保存。虽然它对于帖子非常好。我原本让它在我的本地主机上工作,但是在把它放到网上之后似乎已经以某种方式打破了它。

function framework_add_meta_box() {
    $screens = array( 'post', 'page' );

    foreach( $screens as $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 a nonce field so we can check for it 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 is set.
    if ( ! isset( $_POST['framework_meta_box_nonce'] ) ) {
        return;
    }

    // Verify that the nonce is valid.
    if ( ! wp_verify_nonce( $_POST['framework_meta_box_nonce'], 'framework_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;
        }
    }

    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' );

1 个答案:

答案 0 :(得分:1)

验证$_POST['post_type']值。

另外,我不会使用$_POST值而是使用函数中的第二个参数,如下所示:

function framework_save_meta_box_data( $post_id, $post ) {
    // Check if our nonce is set.
    if ( ! isset( $_POST['framework_meta_box_nonce'] ) ) {
        return;
    }

    // Verify that the nonce is valid.
    if ( ! wp_verify_nonce( $_POST['framework_meta_box_nonce'], 'framework_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 ( '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 );