【问题标题】:cannot save data to db using wordpress metaboxes无法使用 wordpress 元框将数据保存到数据库
【发布时间】:2012-06-26 10:48:09
【问题描述】:

我已经创建了这段代码,它应该使用 wordpress 元框功能来保存数据库中帖子的额外数据。

//Price list meta boxes
add_action( 'add_meta_boxes', 'cd_meta_box_add' );
function cd_meta_box_add() {add_meta_box( 'price-list-id', 'Price List', 'cd_meta_box_cb', 'post', 'normal', 'high' );}

function cd_meta_box_cb()
{  
    // $post is already set, and contains an object: the WordPress post
    global $post;
    $values = get_post_custom( $post->ID );

        // 
        $plheading = isset( $values['price_list_heading'] ) ? esc_attr( $values['price_list_heading'][0] ) : '';

        $plcategory1 = isset( $values['price_list_category1'] ) ? esc_attr( $values['price_list_category1'][0] ) : '';
        $plcategoryitems1 = isset( $values['price_list_items_category1'] ) ? esc_attr( $values['price_list_items_category1'][0] ) : '';

        $plcategory2 = isset( $values['price_list_category2'] ) ? esc_attr( $values['price_list_category2'][0] ) : '';
        $plcategoryitems2 = isset( $values['price_list_items_category2'] ) ? esc_attr( $values['price_list_items_category2'][0] ) : '';

        $plcategory3 = isset( $values['price_list_category3'] ) ? esc_attr( $values['price_list_category3'][0] ) : '';
        $plcategoryitems3 = isset( $values['price_list_items_category3'] ) ? esc_attr( $values['price_list_items_category3'][0] ) : '';


    // We'll use this nonce field later on when saving.
    wp_nonce_field( 'my_meta_box_nonce', 'meta_box_nonce' );
    ?>

        <p>
            <label for="price_list_heading">Price list heading:</label>
            <input type="text" name="price_list_heading" id="price_list_heading" value="<?php echo $plheading; ?>" />
        </p>

        <p>
            <label for="price_list_category1">Category1 Title:</label>
            <input type="text" name="price_list_category1" id="price_list_category1" value="<?php echo $plcategory1; ?>" />
            <textarea style="width: 100%;" rows="3" name="price_list_items_category1" id="price_list_items_category1"><?php echo $plcategoryitems1; ?></textarea>
            <span style="display: block; font-weight: bold; text-align: right;">Example: Rhine Riesling1|0,75 l|9,50 &euro;</span>
        </p>

        <p>
            <label for="price_list_category2">Category2 Title:</label>
            <input type="text" name="price_list_category2" id="price_list_category2" value="<?php echo $plcategory2; ?>" />
            <textarea style="width: 100%;" rows="3" name="price_list_items_category2" id="price_list_items_category2"><?php echo $plcategoryitems2; ?></textarea>
            <span style="display: block; font-weight: bold; text-align: right;">Example: Rhine Riesling1|0,75 l|9,50 &euro;</span>
        </p>

        <p>
            <label for="price_list_category3">Category3 Title:</label>
            <input type="text" name="price_list_category3" id="price_list_category3" value="<?php echo $plcategory3; ?>" />
            <textarea style="width: 100%;" rows="3" name="price_list_items_category3" id="price_list_items_category3"><?php echo $plcategoryitems3; ?></textarea>
            <span style="display: block; font-weight: bold; text-align: right;">Example: Rhine Riesling1|0,75 l|9,50 &euro;</span>
        </p>

    <?php
}

//Saving price list
add_action( 'save_post', 'cd_meta_box_save' );  
function cd_meta_box_save( $post_id )  
{  
        // Bail if we're doing an auto save
        if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;

        // if our nonce isn't there, or we can't verify it, bail
        if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'my_meta_box_nonce' ) ) return;

        // if our current user can't edit this post, bail
        if( !current_user_can( 'edit_post' ) ) return;


        // Make sure your data is set before trying to save it
        if( isset( $_POST['price_list_heading'] ) )
            update_post_meta( $post_id, 'price_list_heading', esc_attr( $_POST['price_list_heading'] ) );


        //
        if( isset( $_POST['price_list_category1'] ) )
            update_post_meta( $post_id, 'price_list_category1', esc_attr( $_POST['price_list_category1'] ) );

        if( isset( $_POST['price_list_items_category1'] ) )
            update_post_meta( $post_id, 'price_list_items_category1', esc_attr( $_POST['price_list_items_category1'] ) );


        //
        if( isset( $_POST['price_list_category2'] ) )
            update_post_meta( $post_id, 'price_list_category2', esc_attr( $_POST['price_list_category2'] ) );

        if( isset( $_POST['price_list_items_category2'] ) )
            update_post_meta( $post_id, 'price_list_items_category2', esc_attr( $_POST['price_list_items_category2'] ) );


        //
        if( isset( $_POST['price_list_category3'] ) )
            update_post_meta( $post_id, 'price_list_category3', esc_attr( $_POST['price_list_category3'] ) );

        if( isset( $_POST['price_list_items_category3'] ) )
            update_post_meta( $post_id, 'price_list_items_category3', esc_attr( $_POST['price_list_items_category3'] ) );
}

我没有将我在文本框中写入的内容保存在 DB 中,而是在所有字段中找到“数组”!到底是怎么回事?我错过了什么?

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    想通了$plheading = isset( $values['price_list_heading'] ) ? esc_attr( $values['price_list_heading'][0] ) : '';

    我错过了[0]

    顺便说一句,在该教程中的某些示例中缺少 [0] :)

    【讨论】:

      【解决方案2】:

      请看一下步骤,看看有没有漏掉什么,这里是链接http://www.farinspace.com/how-to-create-custom-wordpress-meta-box/

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多