wordpress wp_insert_user function
我正在为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 ); |
我已经找到了
谢谢,丹
对于我从Wordpress文档中了解的内容,
如果存在,则更新用户;如果不存在,则创建该用户,并且该函数返回新用户的
如果我正确理解了您的问题,则想向当前注册用户添加新的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")
祝你好运!