管理员添加新用户(wordpress)时添加自定义字段?

时间:2012-11-18 13:15:15

标签: wordpress admin field add

我很感兴趣当管理员添加新用户(Wordpress)时,如何添加字段(作为输入)。不是在用户注册时,而是仅在管理员添加新用户时。我在网上找到了例如,但仅针对要编辑用户的字段,但我想添加新用户。来自输入的新数据 - 并将它们保存在用户元表中。有人在做这个练习。

3 个答案:

答案 0 :(得分:8)

重新审视这个有趣的问题,我会说不,这是不可能的

分析文件/wp-admin/user-new.php,可以使用无操作挂钩。即使遵循<form>中使用的功能也无处可寻。

一个可能的脏解决方案是使用jQuery注入字段。

将新用户添加到数据库时,我们可以使用钩子user_profile_update_errors来更新用户元(也称为hack)。

更新:可以添加新字段:

add_action( 'user_new_form', function( $type ){
    if( 'add-new-user' !== $type )
        return;
    echo '<input type="text" name="custom_user_field" id="custom_user_field" value=""/>';
});

要获取发布的数据并进行处理,请使用:

add_action( 'user_profile_update_errors', function( $data )
{
    if ( is_wp_error( $data->errors ) && ! empty( $data->errors ) ) 
        return;

    # Do your thing with $_POST['custom_user_field'] 
    wp_die( sprintf( '<pre>%s</pre>', print_r( $_POST, true ) ) );
});

答案 1 :(得分:2)

由于wordpress 3.7有一个钩子。

使用钩子“user_new_form”。它在插入提交按钮之前触发。 这样,您就可以在新用户表单中显示自己的自定义字段。

答案 2 :(得分:0)

我发现&#34; user_profile_update_errors&#34;这里建议的钩子没有给我user_id允许我保存元字段。 因此我改为使用&#34; user_register&#34;如下面的代码所示。

希望这会帮助别人。

//  add fields to new user form
add_action( 'user_new_form', 'mktbn_user_new_form' );

function mktbn_user_new_form()
{
?>    

<table class="form-table">
    <tbody>
    <tr class="form-field">
        <th scope="row"><label for="business_name">Company </label></th>
        <td><input name="business_name" type="text" id="business_name" value=""></td>
    </tr>    

    <tr class="form-field">
        <th scope="row"><label for="business_type">Profession </label></th>
        <td><input name="business_type" type="text" id="business_type" value=""></td>
    </tr>    

    <tr class="form-field">
        <th scope="row"><label for="user_mobile">Mobile </label></th>
        <td><input name="user_mobile" type="text" id="user_mobile" value=""></td>
    </tr>

    </tbody>
</table>


<?php   
}

//  process the extra fields for new user form
add_action( 'user_register', 'mktbn_user_register', 10, 1 );

function mktbn_user_register( $user_id )
{
    mikakoo_log( 'user_register: ' . $user_id );

    if ( isset( $_POST['business_name'] ))
    {
        update_user_meta($user_id, 'business_name', $_POST['business_name']);
    }

    if ( isset( $_POST['business_type'] ))
    {
        update_user_meta($user_id, 'business_type', $_POST['business_type']);
    }

    if ( isset( $_POST['user_mobile'] ))
    {
        update_user_meta($user_id, 'user_mobile', $_POST['user_mobile']);
    }
}
相关问题