使用 OOP 方式的 WordPress 自定义元框不保存数据

WordPress custom meta box using OOP way doesn't save data

这是我第一次使用 OOP 方式构建元框,问题是当我保存 post(产品)或更新它时,输入文本没有保存到数据库中。这是我的代码:

<?php 

/**
* 
*/
class Custom_Meta_Boxes{

    public function __construct(){

        add_action( 'add_meta_boxes', array( $this, 'iam_add_meta_box' ) );
        add_action( 'save_post', array( $this, 'iam_save_meta_box_data' ) );

    }

    /**
     * Adds a meta box to the post editing screen
     */
    public function iam_add_meta_box(){

        add_meta_box(
            'custom_meta_box',
            __( 'Meta Box Title', 'iamtheme' ),
            array( $this, 'iam_display_custom_meta_box' ),
            'post',
            'normal',
            'high'
        );

    }

    /**
     * Render Meta Box content.
     */
    public function iam_display_custom_meta_box() {

        $html = '';

        // Add an nonce field so we can check for it later.
        wp_nonce_field( 'iam_nonce_check', 'iam_nonce_check_value' );

        $html = '<label for="link-text" class="prfx-row-title">Link: </label>';
        $html .= '<input type="text" name="link-text" id="link-text" value="' . get_post_meta( get_the_ID(), 'link-text', true ) . '" placeholder="Enter your link here." />';

        echo $html;
    }

    /**
     * Save the meta when the post is saved.
     */
    public function iam_save_meta_box_data( $post_id ){

        var_dump( $post_id );

        if ( $this->iam_user_can_save( $post_id, 'iam_nonce_check_value' ) ){

            // Checks for input and sanitizes/saves if needed
            if( isset( $_POST[ 'link-text' ] ) && 0 < count( strlen( trim( $_POST['link-text'] ) ) ) ) {

                update_post_meta( $post_id, 'link-text', sanitize_text_field( $_POST[ 'link-text' ] ) );

            }

        }

    }

    /**
     * Determines whether or not the current user has the ability to save meta 
     * data associated with this post.
     *
     * @param       int     $post_id    The ID of the post being save
     * @param       bool                Whether or not the user has the ability to save this post.
    */
    public function iam_user_can_save( $post_id, $nonce ){

        var_dump( $post_id );

        // Checks save status
        $is_autosave = wp_is_post_autosave( $post_id );
        $is_revision = wp_is_post_revision( $post_id );
        $is_valid_nonce = ( isset( $_POST[ $nonce ] ) && wp_verify_nonce( $_POST[ $nonce ], 'iam_nonce_check' ) ) ? 'true' : 'false';

        // Return true if the user is able to save; otherwise, false.
        if ( $is_autosave || $is_revision || !$is_valid_nonce ) {
            return;
        }

    }

}

// Instantiate theme
if ( class_exists( 'Custom_Meta_Boxes' ) ){
    $i_am = new Custom_Meta_Boxes();
}

?>

请你告诉我这段代码有什么问题,谢谢你的帮助。

此致。

这是个小逻辑错误,如果用户提交了存储数据,你忘了return true :)
您的函数 iam_user_can_save 代码应如下所示:

public function iam_user_can_save( $post_id, $nonce ){

    var_dump( $post_id );

    // Checks save status
    $is_autosave = wp_is_post_autosave( $post_id );
    $is_revision = wp_is_post_revision( $post_id );
    $is_valid_nonce = ( isset( $_POST[ $nonce ] ) && wp_verify_nonce( $_POST[ $nonce ], 'iam_nonce_check' ) ) ? 'true' : 'false';

    // Return true if the user is able to save; otherwise, false.
    if ( $is_autosave || $is_revision || !$is_valid_nonce ) {
        return false; //return false here
    }
    return true; //return true here

}

这样,函数中iam_save_meta_box_data满足以下条件,

if ( $this->iam_user_can_save( $post_id, 'iam_nonce_check_value' ) ){

update_post_meta将运行正确。