WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] using wp_signon for a login form in a template (2 posts)

  1. Wabsnasm
    Member
    Posted 4 years ago #

    I'm trying to create a template in my theme that only shows content to users who are logged in. If the user is not, then he gets a login form.

    Here's the code:

    <?php
    			if( !is_user_logged_in() )
    			{
    				// try loggin them in
    				wp_signon($_POST, true);
    			}
    
    			if( !is_user_logged_in() ):?>
    				<div class="loginform">
    					<p>Please enter your username and password to continue</p>
    					<div class="formbox">
    						<form method="post">
    							<label for="log">Username:</label> <input type="text" name="log"><br />
    							<label for="pwd">Password:</label> <input type="password" name="pwd"><br />
    							<input type="hidden" name="rememberme" value="true">
    							<input type="submit" value="Log in" name="wp-submit">
    						</form>
    					</div>
    				</div>
    			<?php
    			else:
    			?>
    				<div class="pagebody">
    					<?php // the loop goes here ?>
    				</div>
    				<?php get_sidebar(); ?>
    			<?php
    			endif;

    So, the form POSTs the form data back to the same page, where supposedly it is picked up by wp_signon, and the user is logged in.

    I've searched on Google and these forums, but I can find barely anything on using wp_signon. If anyone could point me towards some examples, or solve my code above that would be absolutely grand.

  2. Wabsnasm
    Member
    Posted 4 years ago #

    I didn't have to use wp_signon at all anyway.

    My solution was to create a login form in a template, submit the form to /wp-login.php with a redirect back to the_permalink().

    Job done!

    For those that want to know:

    if( !is_user_logged_in() ):?>
    				<div class="loginform">
    					<p>Please enter your username and password to continue</p>
    					<form method="post" action="<?php bloginfo('url') ?>/wp-login.php">
    						<div class="formbox">
    
    							<label for="user_login">Username:</label> <input type="text" name="log" id="user_login"><br />
    							<label for="user_pass">Password:</label> <input type="password" name="pwd" id="user_pass"><br />
    							<input type="hidden" name="rememberme" value="forever">
    							<input type="hidden" name="redirect_to" value="<?php the_permalink(); ?>">
    
    						</div>
    						<p><input type="image" src="<?php bloginfo('template_directory') ?>/images/login-button.gif" value="Log in" name="wp-submit">
    					</form>
    				</div>
    			<?php
    			else:
    			?>
    				<div class="pagebody">
    					<?php // the loop goes here ?>
    				</div>
    				<?php get_sidebar(); ?>
    			<?php
    			endif;

Topic Closed

This topic has been closed to new replies.

About this Topic