如何使用核心 php 在 WordPress 中注册后自动登录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19949876/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 19:25:05  来源:igfitidea点击:

How to Auto Login After Registration in WordPress with core php

wordpress

提问by smallerik

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 default, WordPress sends you a username and a password, then you must log in manually. This is a total pain. How can i overcome this. I have my own registration page(core php page) which successfully adds users into DB. But the point is, i should avoid users to login again. Once registration is done, it should automatically redirects to home page or profile page. I am a new bee to wordpress functionalities. It would be grateful if someone(who have knowledge on core functionality of wordpress) atleast suggests a way/solution. Looking forward. Thanks

几天来,我一直在尝试让刚刚注册到我的 WordPress 网站的用户自动登录,然后将他们重定向到我选择的 URL。默认情况下,WordPress 会向您发送用户名和密码,然后您必须手动登录。这是一种彻底的痛苦。我怎样才能克服这个。我有自己的注册页面(核心 php 页面),它成功地将用户添加到数据库中。但关键是,我应该避免用户再次登录。注册完成后,它应该会自动重定向到主页或个人资料页面。我是 wordpress 功能的新手。如果有人(了解 wordpress 核心功能的知识)至少提出一种方法/解决方案,我们将不胜感激。期待。谢谢

采纳答案by smallerik

Thanks for your support guys..i did on my own with the following code..thanks for your time and support :)

感谢您的支持家伙..我用以下代码自己做了..感谢您的时间和支持:)

<i>$getdetails= mysql_fetch_array(mysql_query("SELECT * FROM `wp_users` WHERE `ID`='$user_id'"));
$username=$getdetails['user_login'];


$creds = array();
$creds['user_login'] = $username;
$creds['user_password'] = $password;
$creds['remember'] = true;

    $user = wp_signon( $creds, false );
    if ( is_wp_error($user) ){
        echo $user->get_error_message();
    }else{
        wp_redirect( home_url() );
    }

回答by ravi patel

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

// 添加 function.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' );

回答by diggy

Following is based on how WooCommerce creates a new user and logs him in:

以下是基于 WooCommerce 如何创建新用户并让他登录:

$user_pass = esc_attr( $_POST['account_password'] );

$new_user_data = array(
    'user_login' => $_POST['account_username'],
    'user_pass'  => $user_pass,
    'user_email' => $_POST['account_email'],
    'role'       => 'subscriber'
);

$user_id = wp_insert_user( $new_user_data );

// Set the global user object
$current_user = get_user_by( 'id', $user_id );

// set the WP login cookie
$secure_cookie = is_ssl() ? true : false;
wp_set_auth_cookie( $user_id, true, $secure_cookie );

to redirect use wp_safe_redirect, e.g.

重定向使用wp_safe_redirect,例如

wp_safe_redirect( home_url( '/' ) );
exit;