如何在编辑用户页面中添加自定义字段

时间:2018-05-03 11:30:27

标签: php wordpress woocommerce

我想在编辑用户个人资料页面的联系信息部分添加新字段。 我正在使用此代码,但它是在帐户管理部分后添加新部分,但我想在联系信息部分的电子邮件后添加它。

<?php

add_action( 'show_user_profile', 'yoursite_extra_user_profile_fields' );
add_action( 'edit_user_profile', 'yoursite_extra_user_profile_fields' );

function yoursite_extra_user_profile_fields( $user ) {
?>
    <h3><?php _e("User profile information", "blank"); ?></h3>
    <table class="form-table">
        <tr>
            <th><label for="phone"><?php _e("Operational Manager Email"); ?></label></th>
            <td>
                <input type="text"
                       name="operational_email"
                       class="regular-text" 
                       value="<?php echo esc_attr( get_the_author_meta( 'operational_email', $user->ID ) ); ?>"/>
                <br />
                <span class="description">
                    <?php _e("Please enter opertional manager email.">
                </span>
            </td>
        </tr>
    </table>
<?php
}

add_action( 'personal_options_update', 'yoursite_save_extra_user_profile_fields' );
add_action( 'edit_user_profile_update', 'yoursite_save_extra_user_profile_fields' );

function yoursite_save_extra_user_profile_fields( $user_id ) {
    $saved = false;
    if ( current_user_can( 'edit_user', $user_id ) ) {
        update_user_meta(
            $user_id,
            'operational_email', 
            $_POST['operational_email']
        );
        $saved = true;
    }
    return true;
}

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:2)

我在最近的项目中完成了我的主题,但我是从过滤器名称'user_contactmethods'中完成的 阅读文档:https://codex.wordpress.org/Plugin_API/Filter_Reference/contactmethods

{ _id: 5aeae87d44b0a40a63cdb7fc,
 type: 'website',
 description: 'hhhh',
 index: 8,
 __v: 0,
 required: false,
 flag: '',
 content: ''
}
相关问题