[英]Inserting users into WordPress table using PHP
I am trying to add users to the wp_users
table in WordPress using PHP. 我正在尝试使用PHP将用户添加到WordPress中的
wp_users
表中。 I think I have the right code (or maybe I don't) but I can't seem to get it to run. 我认为我有正确的代码(或者也许我没有),但似乎无法运行它。 So either I have done something wrong or I am a bit confused (well I am definitely confused).
因此,要么我做错了什么,要么我有点困惑(嗯,我肯定感到困惑)。
Basically I have this code in a PHP file under WordPress plugins. 基本上我在WordPress插件下的PHP文件中有此代码。 I am then entering the code in the URL as:
然后,我在URL中输入以下代码:
localhost:8888/xxx/xxx/plugins/register_user.php.
Will this work or what do I need to do? 请问这项工作或我需要做什么? When I check MySQL afterwards it has not entered the user.
当我之后检查MySQL时,它还没有输入用户。 Am I doing something blatantly wrong?
我在做些公然的错吗?
I have never done this before, and don't know PHP very well so please explain things clearly and for someone who is stupid. 我以前从未做过此事,并且对PHP不太了解,所以请为那些愚蠢的人清楚地解释一下。 Here is the code.
这是代码。
<?php # Script 9.3 - register.php
$page_title = 'Register';
add_action('init', 'create_user');
function create_user() {
$username = 'username123';
$password = 'pasword123';
$email = 'drew@example.com';
$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;
}
}
}
To Create a user in wordpress you can use the function WP Create User. 要在wordpress中创建用户,可以使用功能WP Create User。 You seem to be complicating the code a little bit.
您似乎使代码有些复杂。 You can read about creating users in the Wordpress Codex here
您可以在此处阅读有关在Wordpress Codex中创建用户的信息
<?php wp_create_user( $username, $password, $email ); ?>
Here is how wordpress handles it in the wp-admin/upgrade-functions.php: 这是wordpress在wp-admin / upgrade-functions.php中处理它的方式:
$user_id = username_exists( $user_name );
if ( !$user_id and email_exists($user_email) == false ) {
$random_password = wp_generate_password( $length=12, $include_standard_special_chars=false );
$user_id = wp_create_user( $user_name, $random_password, $user_email );
} else {
$random_password = __('User already exists. Password inherited.');
}
Hope This Helps 希望这可以帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.