如何在注册后自动登录?

时间:2019-05-15 作者:gloom

我使用创建用户

 $user_id = wp_create_user($username, $password, $email);
创建用户后,我希望他们自动登录,因此根据ravi patel, 我在函数中添加了以下代码。php。

function auto_login_new_user( $user_id ) {
    wp_set_current_user($user_id);
    wp_set_auth_cookie($user_id);
    $user = get_user_by( \'id\', $user_id );
    do_action( \'wp_login\', $user->user_login );//`[Codex Ref.][1]
    wp_redirect( home_url() ); // You can change home_url() to the specific URL,such as "wp_redirect( \'http://www.wpcoke.com\' )";
    exit;
}
add_action( \'user_register\', \'auto_login_new_user\' );
但正如所料,出现了以下警告。


Warning: Cannot modify header information - headers already sent by (output started at /opt/lampp/htdocs/vuewordpress/wp-includes/link-template.php:3840) in /opt/lampp/htdocs/vuewordpress/wp-includes/pluggable.php on line 928
我如何摆脱这种情况,并重定向同一页面上的用户,使其不再尝试注册?

我明白一件事,我需要在写头之前运行代码。也许我需要把这件事牵扯进去init. 但我不知道怎么做。

任何帮助都是值得赞赏的!!

1 个回复
最合适的回答,由SO网友:Sachin 整理而成

你试过这个吗?

function auto_login_new_user( $user_id ) {
    wp_set_current_user($user_id);
    wp_set_auth_cookie($user_id);
    wp_redirect( home_url() ); // You can change home_url() to the specific URL,such as "wp_redirect( \'http://www.wpcoke.com\' )";
    exit();
}
add_action( \'user_register\', \'auto_login_new_user\' );
摘自SO答案:WordPress auto login after registration not working

相关推荐

Redirection on domain name

我目前正在调试一个荷兰WP网站https://glaszetterdirect.nl问题是,我从以下位置收到了不需要的重定向:http://www.glaszetterdirect.nl 到https://glaszetterdirect.nl/wp-admin我真的不明白发生了什么事。我已经检查了以下内容:.htaccesswp-config.phpwp_options db表格(siteurl 和home 两者:https://glaszetterdirect.nl)</已选中并删除wp_old