1

我正在一个网站上工作,我有一个前端注册表单,它可以正常工作,但现在我在自动登录和成功注册后重定向时遇到了困难。我在我的主题函数文件中尝试了一些插件和代码,但似乎都没有。那么如何在注册成功后自动登录呢?任何帮助将不胜感激。

<?php
if(get_option('users_can_register')) {
//Check whether user registration is enabled by the administrator
?>

<?php

if($_POST){
    //We shall SQL escape all inputs
    $username = $wpdb->escape($_REQUEST['username']);
    if(empty($username)) {
        echo "<span style='color:#FF0000'><strong>Error..</strong></span><br /><br />You have to fill in the username.";
        exit();
    }
    $email = $wpdb->escape($_REQUEST['email']);
    if(!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/", $email)) {
        echo "<span style='color:#FF0000'><strong>Error..</strong></span><br /><br />please use a calid e-mailadress.";
        exit();
    }

    $pass1 = $wpdb->escape($_REQUEST['pass1']);
$pass2 = $wpdb->escape($_REQUEST['pass2']);
if ($pass1 != $pass2){
    echo "<span style='color:#FF0000'><strong>Error..</strong></span><br /><br />please use a passwords don't match.";
        exit();

}
$random_password = $pass1;
    $status = wp_create_user( $username, $random_password, $email );
    if ( is_wp_error($status) )
        echo "<span style='color:#FF0000'><strong>Feil..</strong></span><br /><br />Username allready exist. please try another one.";
    else {
        $from = get_option('admin_email');
        $headers = 'From: '.$from . "\r\n";
        $subject = "Registration ok!";
        $msg = "Welcome, you are now registered. Here is your username and password.\Info:\Username: $username\Password: $random_password";
        wp_mail( $email, $subject, $msg, $headers );
        echo "<strong>You are now registered. An e-mail is now sent to you with your username and password..";
    }

    exit();

}
 else
{
//Embed the register form and javascript here

?>


<div id="result"></div>
<div style="padding-top:5px;"><h2 style="font-size:16px;color:#06f;">Register</h2></div>

<form id="wp_signup_form" action="" method="post">
<p><label>Username<br />
<input type="text" name="username" style="width:250px; margin-bottom:3px;"></label></p>
<p><label>E-Mail<br />
<input type="text" name="email" style="width:250px; margin-bottom:3px;"></label></p>
<p><label>Password<br />
<input type="password" name="pass1" style="width:250px; margin-bottom:3px;"></label></p>
<p><label>Repeat Password<br />
<input type="password" name="pass2" style="width:250px; margin-bottom:3px;"></label></p>
<br />
<input type=checkbox name="termsnp">By registering I agree to the terms and policy<br /><br />
<input type="submit" id="submitbtn" name="submit" value="Register" class="knapp" style="padding:8px;">
</form>
<script type="text/javascript">                         
$("#submitbtn").click(function() {
$('#result').html('<img src="<?php bloginfo('template_url'); ?>/images/loader.gif" class="loader" />').fadeIn();
var input_data = $('#wp_signup_form').serialize();
$.ajax({
type: "POST",
url:  "<?php echo "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; ?>",
data: input_data,
success: function(msg){
$('.loader').remove();
$('
<div>').html(msg).appendTo('div#result').hide().fadeIn('slow');
}
});
return false;

});
</script>
<?php
// embedded

}
?>

<?php
    }
?>
4

2 回答 2

2

注册后自动登录使用此代码

 $userdata = array(
        'user_login' => esc_attr($username),
        'user_email' => esc_attr($email),
        'user_pass' => esc_attr($password),
        'first_name' => esc_attr($first_name),
        'last_name' => esc_attr($last_name),
        'display_name' => esc_attr($first_name.' '.$last_name),
    );
    $register_user = wp_insert_user($userdata);
    if (!is_wp_error($register_user)) {
        wp_set_current_user( $register_user, $username );
        wp_set_auth_cookie( $register_user );
        do_action( 'wp_login', $username );
    } else{
         echo $register_user->get_error_message();
    }

和重定向使用这个

wp_redirect( site_url('my-account') ); exit;

用您成功的网址替换 site_url('my-account')

于 2017-02-12T10:01:15.770 回答
0

我没有找到要使用的更正编码,所以我最终做的是使用插件 Formidable Pro 创建一个注册表单,该注册表单在成功注册后登录并重定向。- http://formidablepro.com/

于 2013-01-31T07:17:29.087 回答