How to Auto Login After Registration in WordPress with core php

后端 未结 3 1179
轻奢々
轻奢々 2020-12-23 11:11

I\'ve been trying for days now to take users who have just registered to my WordPress site and automatically log them in and then redirect them to a URL of my choice. By def

3条回答
  •  猫巷女王i
    2020-12-23 11:22

    // Add on function.php for Auto login after register and redirect to a home page. varified this code

    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' );
    

提交回复
热议问题