关于php:wordpress wp_insert_user函数

wordpress wp_insert_user function

我正在为WordPress创建自定义注册表格。我的问题是添加自定义用户元。我相信功能wp_insert_user仅允许您在WordPress用户表中添加默认字段。

这是我当前的测试代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$username = '12344';
$password = '1111';
$user_data = array(
'ID' => '',
'user_pass' => $password,
'user_login' => $username,
'display_name' => $loginName,
'first_name' => $firstName,
'last_name' => $lastName,
'role' => get_option('default_role') ,
'user_secondry_email' => '[email protected]'// Use default role or another role, e.g. 'editor'
);
$user_id = wp_insert_user( $user_data );
wp_hash_password( $password );

我已经找到了add_user_meta函数,但这需要一个ID来添加元数据。显然,尚未创建用户,因此他们将没有ID。关于如何解决这个问题的任何想法?

谢谢,丹


对于我从Wordpress文档中了解的内容,ID字段是可选的。

如果存在,则更新用户;如果不存在,则创建该用户,并且该函数返回新用户的ID


如果我正确理解了您的问题,则想向当前注册用户添加新的user_meta。

这就是我的做法。

1
2
3
4
5
6
7
8
9
10
11
12
// this code insert the user, same on your code above just remove the ID field :)
$new_userid = wp_insert_user( $user_data );

$is_success = add_user_meta( $new_userid, 'position', 'programmer', true );
if( $is_success  ) {
   echo 'Successfully added';
} else {
   echo 'Error on user creation';
}

**NOTE:**
$new_userid = Is the return value from adding our new user and use that ID in adding new user meta

希望此帮助和其他用户寻求相同的解决方案。


从阵列中完全删除ID值,它应该可以工作。 (例如移除'ID'=>'')

此外,我认为" user_secondary_email"不是新用户数组的允许字段。字段列表在这里:http://codex.wordpress.org/Function_Reference/wp_insert_user

(在您的示例中," user_secondry_email"缺少一个" a")

祝你好运!