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

如何在WordPress后台user-new.php添加WooCommerce billing_phone字段并保存

WordPress后台新增用户表单添加WooCommerce账单手机号字段实现方案

原代码仅完成了前端输入框的渲染,缺少字段标识、提交后的权限校验与数据存储逻辑,因此无法生效。完整实现代码如下:

完整实现代码

将以下代码添加到站点当前激活主题的functions.php文件,或自定义功能插件的代码文件中即可生效:

// 1. 在新增用户页面渲染账单手机号输入字段
add_action( 'user_new_form', function( $type ){
    if( 'add-new-user' !== $type )
        return;
?>
<table class="form-table">
    <tr class="form-field">
        <th scope="row"><label for="billing_phone">账单手机号</label></th>
        <td>
            <input type="tel" name="billing_phone" id="billing_phone" value="" />
            <p class="description">填写后自动同步到WooCommerce用户账单地址信息,无需二次编辑</p>
        </td>
    </tr>
</table>
<?php
});

// 2. 保存账单手机号到用户元数据
add_action( 'user_register', function( $user_id ){
    // 校验操作权限,防止非法提交
    if ( ! current_user_can( 'create_users' ) ) {
        return;
    }
    if ( isset( $_POST['billing_phone'] ) ) {
        $clean_phone = sanitize_text_field( $_POST['billing_phone'] );
        // WooCommerce默认读取billing_phone作为账单手机号字段,存储后自动同步所有关联场景
        update_user_meta( $user_id, 'billing_phone', $clean_phone );
    }
});

功能验证

进入WordPress后台→用户→添加用户,即可看到新增的账单手机号输入框,填写后创建用户,对应账号的WooCommerce账单手机号、前端我的账户手机号字段会自动填充对应值。

内容的提问来源于stack exchange,提问作者Lubo Masura

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.18 15:06:10