Metabox现时值不允许更新

时间:2013-06-24 作者:mantis

更正在自定义帖子类型上保存metabox数据时遇到问题。我想我通过在这篇帖子上应用建议来解决这个问题:Metabox nonce PHP notice

这确实消除了警告,但使我无法更新元数据库。以下是相关代码:

function save_goal_info_meta( $post_id ) {
    global $goal_info_meta_fields;

    // verify nonce
    if ( ! isset( $_POST[\'product_noncename\'] ) || ! wp_verify_nonce( $_POST[\'goal_info_meta_box_nonce\'], basename( __file__ ) ) ) {
        return $post_id;
    }

    // check autosave
    if ( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE ) {
        return $post_id;
    }

    // check permissions
    if ( \'page\' == $_POST[\'post_type\'] ) {
        if ( ! current_user_can( \'edit_page\', $post_id ) )
            return $post_id;
    } elseif ( ! current_user_can( \'edit_post\', $post_id ) ) {
        return $post_id;
    }

    // loop through fields and save the data
    foreach ( $goal_info_meta_fields as $field ) {
        if ( $field[\'type\'] == \'tax_select\' )
            continue;

        $old = get_post_meta( $post_id, $field[\'id\'], true );
        $new = $_POST[$field[\'id\']];

        if ( isset( $new ) && ! empty( $new ) && ( $new != $old ) ) {
            update_post_meta( $post_id, $field[\'id\'], $new );
        } elseif ( \'\' == $new && $old ) {
            delete_post_meta( $post_id, $field[\'id\'], $old );
        }
    }
}

add_action( \'save_post\', \'save_goal_info_meta\' );
我或多或少理解为什么它没有更新,但如果没有“未定义的索引”,我似乎无法修复它。

$goal_info_meta_fields = array(

   array(
    \'label\' => \'Home Goals by:\',
    \'desc\' => \'Add and select home scorers\',
    \'id\' => $prefix.\'home-scorers\',
    \'type\' => \'home_player_list\'
   ),
   array(
    \'label\' => \'Away Goals by:\',
    \'desc\' => \'Add and select away scorers\',
    \'id\' => $prefix.\'away-scorers\',
    \'type\' => \'away_player_list\'
   )
);
以下是输出:

function show_goal_info_meta_box($field, $meta, $items = null) {
    global $goal_info_meta_fields, $post;
// Use nonce for verification

echo \'<input type="hidden" name="goal_info_meta_box_nonce" value="\'.wp_create_nonce(basename(__FILE__)).\'" />\';

// Begin the field table and loop
echo \'<table class="admin-table">\';
foreach ($goal_info_meta_fields as $field) {
    // get value of this field if it exists for this post
    $meta = get_post_meta($post->ID, $field[\'id\'], true);
    // begin a table row with
    echo \'<tr>
            <th><label for="\'.$field[\'id\'].\'">\'.$field[\'label\'].\'</label></th>
            <td>\';
    switch($field[\'type\']) {


        // home_player_list
    case \'home_player_list\':
        echo \'<a class="repeatable-add button" href="#">+</a> <span class="description">\'.$field[\'desc\'].\'</span>
            <ul id="\'.$field[\'id\'].\'-repeatable" class="custom_repeatable">\';
        $post_id = get_the_ID();
        $team_id = get_post_meta( $post_id, \'report_home-select\');

        $items = get_users( array (
                \'meta_key\' => \'team-meta\',
                \'meta_value\' => $team_id
            ));
        $i = 0;

        if ($meta) {
            foreach($meta as $row) {

                echo \'<li><span class="sort hndle button">|||</span>\';
                echo \'<select name="\'.$field[\'id\'].\'[\'.$i.\']" style="width:200px;" id="report_home-scorers">
                <option value="">Select One</option>\'; // Select One
                foreach($items as $item) {
                    echo \'<option value="\'.$item->ID.\'"\',$row == $item->ID ? \' selected="selected"\' : \'\',\'>\'.$item->display_name.\'</option>\';
                }
                echo \'</select><a class="repeatable-remove button" href="#">-</a></li>\';

                $i++;
            }
        } else {
            echo \'<li><span class="sort hndle button">|||</span>\';
            echo \'<select name="report_home-scorers[0]" style="width:200px;" id="\'.$field[\'id\'].\'">
            <option value="NULL">Select One</option>\'; // Select One

            foreach ($items as $item) {
                echo \'<option value="" selected="selected">\'.$item->display_name.\'</option>\';
            }

            echo \'</select><a class="repeatable-remove button" href="#">-</a></li>\';

        }
        echo \'</ul>\';
        break;

        // away_player_list
    case \'away_player_list\':
        echo \'<a class="repeatable-add button" href="#">+</a> <span class="description">\'.$field[\'desc\'].\'</span>
            <ul id="\'.$field[\'id\'].\'-repeatable" class="custom_repeatable">\';
        $postid = get_the_ID();
        $team_id = get_post_meta( $postid, \'report_away-select\');

        $items = get_users( array (
                \'meta_key\' => \'team-meta\',
                \'meta_value\' => $team_id
            ));


        $i = 0;
        if ($meta) {
            foreach($meta as $row) {
                echo \'<li><span class="sort hndle button">|||</span>\';
                echo \'<select name="\'.$field[\'id\'].\'[\'.$i.\']" class="chzn-select" style="width:200px;" id="\'.$field[\'id\'].\'" value="\'.$row.\'">
                <option value="">Select One</option>\'; // Select One
                foreach($items as $item) {
                    echo \'<option value="\'.$item->ID.\'"\',$row == $item->ID ? \' selected="selected"\' : \'\',\'>\'.$item->display_name.\'</option>\';
                }
                echo \'</select><a class="repeatable-remove button" href="#">-</a></li>\';

                $i++;
            }
        } else {
            echo \'<li><span class="sort hndle button">|||</span>\';
            echo \'<select name="\'.$field[\'id\'].\'[\'.$i.\']" class="chzn-select" style="width:200px;" id="\'.$field[\'id\'].\'">
            <option value="">Select One</option>\'; // Select One

            foreach ($items as $item) {
                echo \'<option value="" selected="selected">\'.$item->display_name.\'</option>\';
            }

            echo \'</select><a class="repeatable-remove button" href="#">-</a></li>\';

        }
        echo \'</ul>\';
        break;

    } //end switch
    echo \'</td></tr>\';
} // end foreach
echo \'</table>\'; // end table
}

1 个回复
最合适的回答,由SO网友:TheDeadMedic 整理而成

问题是你应该改变product_noncenamegoal_info_meta_box_nonce. 我还做了一些其他更改,注释掉了旧的行以进行比较,这应该可以避免任何进一步的错误(包括未定义的索引)。

// verify nonce
// if ( ! isset( $_POST[\'product_noncename\'] ) || ! wp_verify_nonce( $_POST[\'goal_info_meta_box_nonce\'], basename( __file__ ) ) ) {
if ( ! isset( $_POST[\'goal_info_meta_box_nonce\'] ) || ! wp_verify_nonce( $_POST[\'goal_info_meta_box_nonce\'], basename( __file__ ) ) ) {
    return $post_id;
}

...

// loop through fields and save the data
foreach ( $goal_info_meta_fields as $field ) {
    // if ( $field[\'type\'] == \'tax_select\' )
    if ( $field[\'type\'] == \'tax_select\' || ! isset( $_POST[ $field[\'id\'] ] ) )
        continue;

    $old = get_post_meta( $post_id, $field[\'id\'], true );
    $new = $_POST[ $field[\'id\'] ];

    // if ( isset( $new ) && ! empty( $new ) && ( $new != $old ) ) {
    if ( ! empty( $new ) && $new != $old ) {
        update_post_meta( $post_id, $field[\'id\'], $new );
    } elseif ( \'\' == $new && $old ) {
        delete_post_meta( $post_id, $field[\'id\'], $old );
    }
}

结束