Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wordpress在插件中注册后自动登录_Wordpress_Plugins_Login_Hook_Registration - Fatal编程技术网

Wordpress在插件中注册后自动登录

Wordpress在插件中注册后自动登录,wordpress,plugins,login,hook,registration,Wordpress,Plugins,Login,Hook,Registration,我有一个插件用于wordpress中的注册过程。没有密码字段,但我创建了一个密码字段并插入了密码,而不是在电子邮件中随机发送密码。它使用wp_create_user函数来创建用户 现在,我正在尝试在注册后自动登录,但失败了 我尝试了以下函数,但失败了。请找个人帮忙 wp_set_current_user($user_id); // set the current wp user wp_set_auth_cookie($user_id); // start the cookie for the c

我有一个插件用于wordpress中的注册过程。没有密码字段,但我创建了一个密码字段并插入了密码,而不是在电子邮件中随机发送密码。它使用wp_create_user函数来创建用户

现在,我正在尝试在注册后自动登录,但失败了

我尝试了以下函数,但失败了。请找个人帮忙

wp_set_current_user($user_id); // set the current wp user
wp_set_auth_cookie($user_id); // start the cookie for the current registered user
wp_redirect(home_url());
这是我一直在使用的过程,它并不完整,但我认为它必须如何工作

$status = wp_create_user($username, $user_pass, $email);
$user_data = $wpdb->get_row("SELECT * FROM $users_table where user_login='$username' ");
$user_id = isset($user_data) ? $user_data->ID : 0;

if (is_wp_error($status)) {
$errors[] = language_code('USER_NAME_ALREADY_EXIST_PLEASE_TRY_ANOTHER_ONE');
} else { 
wp_set_current_user($user_id); 
wp_set_auth_cookie($user_id); 
wp_redirect( home_url() ); 
}

在theme functions.php文件中尝试以下代码,它会起作用

function auto_login_new_user( $user_id ) {
    wp_set_current_user($user_id);
    wp_set_auth_cookie($user_id);
    wp_redirect( home_url() );
    exit;
}
add_action( 'user_register', 'auto_login_new_user' );

我以前也试过,但对我不起作用。你使用任何插件进行注册吗?是的,我一直在使用一个插件进行注册。你使用了哪个插件?你能分享你的代码吗?很难发现你是如何尝试的。我在上面分享了一些代码。这还不完整,但我一直在做的是显示。