通过 WordPress 中的 functions.php 自动创建新用户
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19845192/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create new user automatically via functions.php in WordPress
提问by techno
I need to add new user with admin role via code, and I found this code:
我需要通过代码添加具有管理员角色的新用户,我找到了这个代码:
add_action('init', 'add_user');
function add_user() {
$username = 'username123';
$password = 'pasword123';
$email = '[email protected]';
// Create the new user
$user_id = wp_create_user( $username, $password, $email );
// Get current user object
$user = get_user_by( 'id', $user_id );
// Remove role
$user->remove_role( 'subscriber' );
// Add role
$user->add_role( 'administrator' );
}
But when I added it in functions.php
, I got this error :
但是当我添加它时functions.php
,我收到了这个错误:
Fatal error: Call to a member function remove_role()
on a non-object in ..../functions.php on line ...
I also tried this code:
我也试过这个代码:
function fb_wp_insert_user() {
$user_data = array(
'ID' => '',
'user_pass' => wp_generate_password(),
'user_login' => 'dummy',
'user_nicename' => 'Dummy',
'user_url' => '',
'user_email' => '[email protected]',
'display_name' => 'Dummy',
'nickname' => 'dummy',
'first_name' => 'Dummy',
'user_registered' => '2010-05-15 05:55:55',
'role' => get_option('default_role') // Use default role or another role, e.g. 'editor'
);
$user_id = wp_insert_user( $user_data );
}
add_action( 'admin_init', 'fb_wp_insert_user' );
I changed default role to adminstrator
but when I browsed the users, I found this user without any role.
我将默认角色更改为adminstrator
但是当我浏览用户时,我发现该用户没有任何角色。
回答by The Alpha
This is your error
这是你的错误
Fatal error: Call to a member function remove_role() on a non-object in ..../functions.php on line ...
致命错误:在线调用 ..../functions.php 中的非对象上的成员函数 remove_role() ...
It's is because of $user->remove_role( 'subscriber' );
code and it means that, when you are using following code to retrieve the new user
这是因为$user->remove_role( 'subscriber' );
代码,这意味着,当您使用以下代码检索新用户时
$user = get_user_by( 'id', $user_id );
It's not returning a WP_Userobject. So, if you call a method on a non object, this error shows up and it could be because you didn't get an ID
when you used
它没有返回WP_User对象。因此,如果您在非对象上调用方法,则会出现此错误,这可能是因为您ID
在使用时没有得到
$user_id = wp_create_user( $username, $password, $email );
It's possible that, you didn't successfully created a user and in this case the return value could be an object
according to Codex
有可能,您没有成功创建用户,在这种情况下,返回值可能是object
根据Codex
When successful - this function returns the user ID of the created user. In case of failure (username or email already exists) the function returns an error object, with these possible values and messages;
empty_user_login, Cannot create a user with an empty login name.
existing_user_login, This username is already registered.
existing_user_email, This email address is already registered.
成功时 - 此函数返回创建用户的用户 ID。如果失败(用户名或电子邮件已经存在),该函数将返回一个错误对象,其中包含这些可能的值和消息;
empty_user_login, 无法创建登录名为空的用户。
existing_user_login,此用户名已注册。
existing_user_email, 此电子邮件地址已被注册。
SO, when you are creating a user, at first check if the user exist or not like
所以,当你创建一个用户时,首先检查用户是否存在
add_action('init', 'add_my_user');
function add_my_user() {
$username = 'username123';
$email = '[email protected]';
$password = 'pasword123';
$user_id = username_exists( $username );
if ( !$user_id && email_exists($email) == false ) {
$user_id = wp_create_user( $username, $password, $email );
if( !is_wp_error($user_id) ) {
$user = get_user_by( 'id', $user_id );
$user->set_role( 'administrator' );
}
}
}
Also, there is no need for reomving and adding the role, set_role($role)will remove the previous roles of the user and assign the user the new one.
Read more about wp create userand get user byon Codex
. Also, check the wp_generate_password()to use a secured password instead of plain text.
此外,不需要重新添加和添加角色,set_role($role)将删除用户以前的角色并为用户分配新角色。阅读有关wp create user和get user byon 的更多信息Codex
。此外,检查wp_generate_password()以使用安全密码而不是纯文本。
Update :
更新 :
add_useris a WordPress function, so change the name to something else like, add_my_user
.
add_user是一个 WordPress 函数,因此将名称更改为其他名称,例如add_my_user
.
回答by Pat J
Check to make sure that wp_create_user()
actually createdthe user:
检查以确保wp_create_user()
实际创建了用户:
add_action('init', 'add_user');
function add_user() {
$username = 'username123';
$password = 'pasword123';
$email = '[email protected]';
$user = get_user_by( 'email', $email );
if( ! $user ) {
// Create the new user
$user_id = wp_create_user( $username, $password, $email );
if( is_wp_error( $user_id ) ) {
// examine the error message
echo( "Error: " . $user_id->get_error_message() );
exit;
}
// Get current user object
$user = get_user_by( 'id', $user_id );
}
// Remove role
$user->remove_role( 'subscriber' );
// Add role
$user->add_role( 'administrator' );
}
Edited: Per the comments below, it appears that the user has already been created. I've updated the code to check for that. (In essence, now, if the user doesn't already exist, it'll be created.)
已编辑:根据下面的评论,似乎已经创建了用户。我已经更新了代码来检查。(本质上,现在,如果用户不存在,它将被创建。)
References
参考
回答by Jakir Hossain
$userData = array(
'user_login' => 'username',
'first_name' => 'First',
'last_name' => 'Last',
'user_pass' => 'password',
'user_email' => '[email protected]',
'user_url' => '',
'role' => 'administrator'
);
wp_insert_user( $userData );
回答by Soufiane
function kechweb_create_admin_account(){
$user = 'Username'; <br />
$pass = 'Password';<br />
$email = '[email protected]';<br />
//if a username with the email ID does not exist, create a new user account<br />
if ( !username_exists( $user ) && !email_exists( $email ) ) {<br />
$user_id = wp_create_user( $user, $pass, $email ); <br />
$user = new WP_User( $user_id ); <br />
//Set the new user as a Admin <br />
$user->set_role( 'administrator' ); <br />
} } <br />
add_action('init','kechweb_create_admin_account');