How to Autologin after registration
-
Hello everyone,
I am creating a template for registration. But i want to autologin after registration to profile page. profile page template is “template-profile.php”.
I have doing following code.
<?php get_header(); /* Check if users can register. */ $registration = get_option( 'users_can_register' ); /* If user registered, input info. */ if ( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) && $_POST['action'] == 'adduser' ) { $user_pass = $_POST['password']; $user_confirm_pass= $_POST['confirm_password']; $userdata = array( 'user_pass' => $user_pass, 'user_login' => esc_attr( $_POST['user_name'] ), 'first_name' => esc_attr( $_POST['first_name'] ), 'last_name' => esc_attr( $_POST['last_name'] ), 'nickname' => esc_attr( $_POST['nickname'] ), 'user_email' => esc_attr( $_POST['email'] ), 'role' => get_option( 'default_role' ), ); if ( !$userdata['user_login'] ) $error = __('A username is required for registration.', 'frontendprofile'); elseif ( username_exists($userdata['user_login']) ) $error = __('Sorry, that username already exists!', 'frontendprofile'); elseif ( !is_email($userdata['user_email'], true) ) $error = __('You must enter a valid email address.', 'frontendprofile'); elseif ( email_exists($userdata['user_email']) ) $error = __('Sorry, that email address is already used!', 'frontendprofile'); else{ $new_user = wp_insert_user( $userdata ); } } ?> <div class="column eightcol"> <div class="section-title"> <h1><?php _e('Register', 'lovestory'); ?></h1> </div> <?php if ( is_user_logged_in() && !current_user_can( 'create_users' ) ) : ?> <p class="log-in-out alert"> <?php printf( __('You are logged in as <a href="%1$s" title="%2$s">%2$s</a>. You don\'t need another account.', 'frontendprofile'), get_author_posts_url( $curauth->ID ), $user_identity ); ?> <a href="<?php echo wp_logout_url( get_permalink() ); ?>" title="<?php _e('Log out of this account', 'frontendprofile'); ?>"><?php _e('Logout »', 'frontendprofile'); ?></a> </p><!-- .log-in-out .alert --> <?php elseif ( $new_user ) : ?> <p class="alert"> <?php if ( current_user_can( 'create_users' ) ) printf( __('A user account for %1$s has been created.', 'frontendprofile'), $_POST['user-name'] ); else printf( __('Thank you for registering, %1$s.', 'frontendprofile'), $_POST['user-name'] ); printf( __('<br/>Please check your email address. That\'s where you\'ll recieve your login password.<br/> (It might go into your spam folder)', 'frontendprofile') ); ?> </p><!-- .alert --> <?php else : ?> <?php if ( $error ) : ?> <p class="error"> <?php echo $error; ?> </p><!-- .error --> <?php endif; ?> <?php if ( current_user_can( 'create_users' ) && $registration ) : ?> <p class="alert"> <?php _e('Users can register themselves or you can manually create users here.', 'frontendprofile'); ?> </p><!-- .alert --> <?php elseif ( current_user_can( 'create_users' ) ) : ?> <p class="alert"> <?php _e('Users cannot currently register themselves, but you can manually create users here.', 'frontendprofile'); ?> </p><!-- .alert --> <?php endif; ?> <?php if ( $registration || current_user_can( 'create_users' ) ) : ?> <form method="post" id="adduser" class="user-forms" action="https://<?php echo $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; ?>"> <div class="column sixcol"> <div class="field-wrap"> <input style="width:100%" type="text" name="first_name" id="first_name" placeholder="<?php _e('First Name', 'lovestory'); ?>" value="<?php if ( $error ) echo wp_specialchars( $_POST['first_name'], 1 ); ?>"/> </div> </div> <div class="column sixcol last"> <div class="field-wrap"> <input style="width:100%" type="text" name="last_name" id="last_name" placeholder="<?php _e('Last Name', 'lovestory'); ?>" value="<?php if ( $error ) echo wp_specialchars( $_POST['last_name'], 1 ); ?>"/> </div> </div> <div class="clear"></div> <div class="column sixcol"> <div class="field-wrap"> <input style="width:100%" type="text" name="user_name" id="user_name" placeholder="<?php _e('Username', 'lovestory'); ?>" value="<?php if ( $error ) echo wp_specialchars( $_POST['user_name'], 1 ); ?>"/> </div> </div> <div class="column sixcol last"> <div class="field-wrap"> <input style="width:100%" type="text" name="email" id="email" placeholder="<?php _e('Email', 'lovestory'); ?>" value="<?php if ( $error ) echo wp_specialchars( $_POST['email'], 1 ); ?>"/> </div> </div> <div class="clear"></div> <div class="column sixcol"> <div class="field-wrap"> <input style="width:100%" type="password" name="password" id="password" placeholder="<?php _e('Password', 'lovestory'); ?>" value="<?php if ( $error ) echo wp_specialchars( $_POST['password'], 1 ); ?>"/> </div> </div> <div class="column sixcol last"> <div class="field-wrap"> <input style="width:100%" type="password" name="confirm_password" id="confirm_password" placeholder="<?php _e('Confirm Password', 'lovestory'); ?>" value="<?php if ( $error ) echo wp_specialchars( $_POST['confirm_password'], 1 ); ?>"/> </div> </div> <div class="clear"></div> <p class="form-submit"> <?php echo $referer; ?> <input name="adduser" type="submit" id="addusersub" class="submit button" value="<?php if ( current_user_can( 'create_users' ) ) _e('Add User', 'frontendprofile'); else _e('Register', 'frontendprofile'); ?>" /> <?php wp_nonce_field( 'add-user' ) ?> <input name="action" type="hidden" id="action" value="adduser" /> </p><!-- .form-submit --> </form><!-- #adduser --> <?php endif; ?> <?php endif; ?>
Mainly i am confuse in this code.
`<?php elseif ( $new_user ) : ?>
<p class=”alert”>
<?php
if ( current_user_can( ‘create_users’ ) )
printf( __(‘A user account for %1$s has been created.’, ‘frontendprofile’), $_POST[‘user-name’] );
else
printf( __(‘Thank you for registering, %1$s.’, ‘frontendprofile’), $_POST[‘user-name’] );
printf( __(‘
Please check your email address. That\’s where you\’ll recieve your login password.
(It might go into your spam folder)’, ‘frontendprofile’) );
?>
</p><!– .alert –>
‘Right now when user registerd then following message are coming but i want to autologin.
Please help me.
Thanks
- The topic ‘How to Autologin after registration’ is closed to new replies.