You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WordPress自定义注册表单中update_user_meta()不生效求助

问题分析与修复方案

核心错误:用户ID获取逻辑错误

你的代码里调用update_user_meta时,用了wp_get_current_user()获取用户ID,但此时新用户刚创建完成,还没执行登录操作,wp_get_current_user()返回的是匿名用户(ID=0),导致所有元数据更新都没作用在刚创建的用户身上。

直接使用wp_insert_user返回的$new_user_id即可,这才是新注册用户的真实ID。

次要问题与修复点

  1. 表单缺失red_user_country字段
    验证逻辑里检查了$user_country == '',但注册表单里没有对应的输入框,用户注册会直接触发报错。解决方式二选一:

    • 在red_registration_fields函数中添加国家输入框:
      <p>
          <label for="red_user_country"><?php _e('Country'); ?></label>
          <input name="red_user_country" id="red_user_country" type="text" placeholder="Land" class="red_input" required/>
      </p>
      
    • 或者删除验证逻辑里的if($user_country == '')这段代码。
  2. 冗余的wp_update_user调用
    wp_update_user( array ('ID' => $new_user_id));这行代码没有任何实际更新操作,完全可以删除。

  3. 遗漏其他字段的元数据更新
    你只更新了部分账单/配送字段,需要补充公司、邮编、USt-ID、电话号码等字段的元数据更新。

修改后的关键代码片段

替换red_add_new_user函数里的用户元数据更新部分:

if( !empty( $new_user_id ) ) {
    // 更新账单地址字段
    update_user_meta( $new_user_id, 'billing_first_name', $user_first );
    update_user_meta( $new_user_id, 'billing_last_name', $user_last );
    update_user_meta( $new_user_id, 'billing_email', $user_email );
    update_user_meta( $new_user_id, 'billing_address_1', $user_street );
    update_user_meta( $new_user_id, 'billing_postcode', $user_postal_code );
    update_user_meta( $new_user_id, 'billing_country', $user_country );
    update_user_meta( $new_user_id, 'billing_phone', $user_phone );
    update_user_meta( $new_user_id, 'billing_company', $user_company );
    update_user_meta( $new_user_id, 'billing_ustid', $user_ustid ); // 自定义USt-ID字段

    // 更新配送地址字段
    update_user_meta( $new_user_id, 'shipping_first_name', $user_first );
    update_user_meta( $new_user_id, 'shipping_last_name', $user_last );
    update_user_meta( $new_user_id, 'shipping_address_1', $user_street );
    update_user_meta( $new_user_id, 'shipping_postcode', $user_postal_code );
    update_user_meta( $new_user_id, 'shipping_country', $user_country );
} else {
    echo 'User doesnt exist';
}

更简便的批量更新方式

如果觉得多次调用update_user_meta繁琐,可以用批量处理的方式:

$billing_meta = [
    'billing_first_name' => $user_first,
    'billing_last_name' => $user_last,
    'billing_email' => $user_email,
    'billing_address_1' => $user_street,
    'billing_postcode' => $user_postal_code,
    'billing_country' => $user_country,
    'billing_phone' => $user_phone,
    'billing_company' => $user_company,
    'billing_ustid' => $user_ustid,
];

$shipping_meta = [
    'shipping_first_name' => $user_first,
    'shipping_last_name' => $user_last,
    'shipping_address_1' => $user_street,
    'shipping_postcode' => $user_postal_code,
    'shipping_country' => $user_country,
];

// 批量更新账单元数据
array_map(function($key, $value) use ($new_user_id) {
    update_user_meta($new_user_id, $key, $value);
}, array_keys($billing_meta), $billing_meta);

// 批量更新配送元数据
array_map(function($key, $value) use ($new_user_id) {
    update_user_meta($new_user_id, $key, $value);
}, array_keys($shipping_meta), $shipping_meta);

内容的提问来源于stack exchange,提问作者Watch Wolfe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 01:04:50