• 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 &raquo;', '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

Viewing 10 replies - 1 through 10 (of 10 total)
  • Take a gander at the codex here https://codex.www.ads-software.com/Function_Reference/wp_signon

    Right after

    $new_user = wp_insert_user( $userdata );

    Check that $new_user was successful and then pass the username and password and pass it to wp_signon.

    Simple Example (not functional but should give you the idea)

    $creds = array();
    $creds['user_login'] = {USERNAME};
    $creds['user_password'] = {PASSWORD};
    $creds['remember'] = false;
    $user = wp_signon( $creds, false );
    if (! is_wp_error($user) )
     // Do you redirect here however your want to handle the reload or redirect

    Thread Starter dpkagarwal59

    (@dpkagarwal59)

    Thanks justingreerbbi,

    but after apply this code its giving following errror.

    Warning: Cannot modify header information – headers already sent by (output started at /home/weddingrep/public_html/wp-content/themes/lovestory/template-register.php:11) in /home/weddingrep/public_html/wp-includes/pluggable.php on line 919

    Warning: Cannot modify header information – headers already sent by (output started at /home/weddingrep/public_html/wp-content/themes/lovestory/template-register.php:11) in /home/weddingrep/public_html/wp-includes/pluggable.php on line 920

    Warning: Cannot modify header information – headers already sent by (output started at /home/weddingrep/public_html/wp-content/themes/lovestory/template-register.php:11) in /home/weddingrep/public_html/wp-includes/pluggable.php on line 921

    please help me.

    The header error is because there is content being output to the screen before you are attempting to do the redirect. I assume that you are using wp_redirect() or header() to do the redirect and then is why the error is present. Simple move get_header(); to below the form check and it should start working for you.

    Something to note about handling form data in a template file. There is not right or wrong way to handle a form request but there is better and worse ways. To save you the headache always handle post data before the get_header() function.

    Thread Starter dpkagarwal59

    (@dpkagarwal59)

    Thanks Justin for your response.
    But till now i have getting same error and not also auto login in my profile while i have completely remove get_header from my file to check. i want to move after login to “template-profile.php” page. please help me to solve this problem. i am write down my complete code please check.

    <?php
    
    /*
    
    Template Name: Registration
    
    */
    
    ?>
    
    <?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 &raquo;', '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. You can Login Now, %1$s.', 'frontendprofile'), $_POST['user-name'] );
    
    			?>
    			</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:90%"  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:90%"  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:90%"  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:90%"  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:90%"  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:90%"  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; ?>
    
    <!-- REGISTER FORM ENDS HERE -->
    
    </div>
    
    <div class="column fourcol last">
    
    	<?php if(get_option('users_can_register')) { ?>
    
    	<div class="section-title">
    
    		<h1><?php _e('Sign In', 'lovestory'); ?></h1>
    
    	</div>
    
    	<?php } ?>
    
    	<form class="ajax-form formatted-form" action="<?php echo AJAX_URL; ?>" method="POST">
    
    		<div class="message"></div>
    
    		<div class="field-wrap">
    
    			<input type="text" name="user_login" placeholder="<?php _e('Username', 'lovestory'); ?>" />
    
    		</div>
    
    		<div class="field-wrap">
    
    			<input type="password" name="user_password" placeholder="<?php _e('Password', 'lovestory'); ?>" />
    
    		</div>
    
    		<a href="#" class="button submit-button form-button"><?php _e('Sign In', 'lovestory'); ?></a>
    
    		<?php if(ThemexFacebook::isActive()) { ?>
    
    		<a href="<?php echo home_url('?facebook_login=1'); ?>" class="button facebook-login-button form-button" title="<?php _e('Sign in with Facebook', 'lovestory'); ?>">
    
    			<span class="button-icon icon-facebook nomargin"></span>
    
    		</a>
    
    		<?php } ?>
    
    		<div class="loader"></div>
    
    		<input type="hidden" name="user_action" value="login_user" />
    
    		<input type="hidden" class="nonce" value="<?php echo wp_create_nonce(THEMEX_PREFIX.'nonce'); ?>" />
    
    		<input type="hidden" class="action" value="<?php echo THEMEX_PREFIX; ?>update_user" />
    
    	</form>
    
    </div>
    
    <div class="clear"></div>
    
    <?php 
    
    query_posts(array(
    
    	'post_type' => 'page',
    
    	'meta_key' => '_wp_page_template',
    
    	'meta_value' => 'template-register.php'
    
    ));
    
    while(have_posts()) {
    
    	the_post();
    
    	echo '<br />';
    
    	the_content();
    
    	break;
    
    }
    
    ?>
    
    <?php get_footer(); ?>

    Thanks in advanced

    The code you provided still has get_header() and does not contain the wp_signon functionality either.

    Here is a a pastebin containing the things that you need to do: https://pastebin.com/gdWQWCxL

    Thread Starter dpkagarwal59

    (@dpkagarwal59)

    i have code as you say but till now its giving me error. here is a code where you can see my code.

    https://pastebin.com/gAmHA2P2

    And this is screenshot for error.
    https://prntscr.com/5dwdig

    Thanks

    Thread Starter dpkagarwal59

    (@dpkagarwal59)

    Please anyone help me to resolve these issue.

    Thanks in advanced.

    Here is a few things. Please see below:

    1. What is the error telling you? There is something wrong on line 11… Taking a look at it. There is white space at the top of the script ( not using your opening/closing tags correct ). This is where your header already sent is coming from.

    2. ( You will be having an issue with this ). You are not handling the login at all after checking if wp_signon is valid or not.

    The examples are meant to help you understand what needs to happen. They will not work out of the box in most cases.

    ??

    Thread Starter dpkagarwal59

    (@dpkagarwal59)

    Thanks justingreerbbi for your continue support. I have solved problem.

    Great, what was the cause of your issue? It is helpful for others if you share the solution to your issue.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘How to Autologin after registration’ is closed to new replies.