2014-12-30 48 views
0

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

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

我试了下面的功能,但是失败了。请有人帮忙。

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()); 
} 
+0

你可以分享你的代码,它很难检测到您试过怎么样了。 –

+0

我已经在上面分享了一些代码。这不完整,但我已经做了如何显示。 – Nikx

回答

0

尝试下面的代码到你的主题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'); 
+0

我之前也试过,但它不适合我。 – Nikx

+0

你使用任何插件进行注册? –

+0

是的,我一直在使用插件进行注册。 – Nikx