This question already has answers here: WordPress auto login after registration not working (5 answers) Closed 5 years ago.admin管理员组文章数量:1427140
I create an user using
$user_id = wp_create_user($username, $password, $email);
After creating the user I want them to auto login and so according to ravi patel, I have added the following code in my functions.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( '' )";
exit;
}
add_action( 'user_register', 'auto_login_new_user' );
But as expected, the following warning popped up.
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
How do I get rid off this and redirect the user on the same page from they try to sign up?
I understand one thing the I need to run the code before the header is written. May be I need to somehow hook this action in init
. But I dont know how to do it.
Any help is highly appreciable!!
This question already has answers here: WordPress auto login after registration not working (5 answers) Closed 5 years ago.I create an user using
$user_id = wp_create_user($username, $password, $email);
After creating the user I want them to auto login and so according to ravi patel, I have added the following code in my functions.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' )";
exit;
}
add_action( 'user_register', 'auto_login_new_user' );
But as expected, the following warning popped up.
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
How do I get rid off this and redirect the user on the same page from they try to sign up?
I understand one thing the I need to run the code before the header is written. May be I need to somehow hook this action in init
. But I dont know how to do it.
Any help is highly appreciable!!
Share Improve this question asked May 15, 2019 at 22:06 gloomgloom 1851 gold badge3 silver badges12 bronze badges 1- You'd need to share the code responsible for creating users – Ali_k Commented May 16, 2019 at 1:23
1 Answer
Reset to default 9Have you tried this?
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' )";
exit();
}
add_action( 'user_register', 'auto_login_new_user' );
Taken from this SO answer: WordPress auto login after registration not working
本文标签: redirectHow to auto login after registration
版权声明:本文标题:redirect - How to auto login after registration? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745488906a2660513.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论