为CPT添加了元数据框和警告消息

时间:2017-10-30 22:16:22

标签: php wordpress

  

警告消息: 注意:未定义的索引:team_meta_nonce in   /home/rainpeyi/public_html/wp-content/themes/rainastudio/lib/team_reg.php   在第194行

保存Metabox数据并通过我们的屏幕并经过适当的授权验证这一点,因为save_post可以在其他时间触发。

function rs_save_team_meta($post_id, $post) {

    if ( !wp_verify_nonce( $_POST['team_meta_nonce'], plugin_basename(__FILE__) )) {
        return $post->ID;
    }

    // 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;
    }

    // Is the user allowed to edit the post or page
    if ( !current_user_can( 'edit_post', $post->ID )) {
        return $post->ID;
    }


// OK, we're authenticated: we need to find and save the data

// We'll put it into an array to make it easier to loop though.


    $team_meta['_position'] = $_POST['_position'];
    $team_meta['_availability'] = $_POST['_availability'];
    $team_meta['_experience'] = $_POST['_experience'];
    $team_meta['_email'] = $_POST['_email'];

    // Add values of $team_meta as custom fields

    foreach ($team_meta as $key => $value) { // Cycle through the $team_meta array!
        if( $post->post_type == 'revision' ) return; // Don't store custom data twice
        $value = implode(',', (array)$value); // If $value is an array, make it a CSV (unlikely)
        if(get_post_meta($post->ID, $key, FALSE)) { // If the custom field already has a value
            update_post_meta($post->ID, $key, $value);
        } else { // If the custom field doesn't have a value
            add_post_meta($post->ID, $key, $value);
        }
        if(!$value) delete_post_meta($post->ID, $key); // Delete if blank
    }

}

add_action('save_post', 'rs_save_team_meta', 1, 2); // save the custom fields

2 个答案:

答案 0 :(得分:0)

在您的情况下,“未定义的索引”表示POST数组中没有'team_meta_nonce'。您应该检查它是否先设置:

if ( !isset($_POST['team_meta_nonce']) || !wp_verify_nonce( $_POST['team_meta_nonce'], plugin_basename(__FILE__) )) {
    return $post->ID;
}

答案 1 :(得分:0)

更改您在此处使用的操作

我认为您的自定义帖子类型名称是“团队”

然后保存元框的钩子将是

add_action('save_post_team', 'rs_save_team_meta', 1, 2);