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

use*_*630 1 wordpress field admin add

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

bra*_*ilo 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=""/>';
});
Run Code Online (Sandbox Code Playgroud)

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

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 ) ) );
});
Run Code Online (Sandbox Code Playgroud)