1

I successfully inserted a new user to WP database and I want this user to be logged in right after this. I do it like this

User Login Form

<?php  
     function dlf_form() {

?>


<form method="post" id="wp_login_form" action="<?php echo $_SERVER['REQUEST_URI']; ?>">
    <div class="login-form">
        <div class="form-group">
                    <input name="login_name" type="email" class="form-control login-field" value="" placeholder="Email" id="login-name" required/>
            <label class="login-field-icon fui-user" for="login-name"></label>
        </div>

        <div class="form-group">
            <input name="login_password" type="password" class="form-control login-field" value="" placeholder="Password" id="login-pass" required/>
            <label class="login-field-icon fui-lock" for="login-pass"></label>
        </div>
            <input class="btn btn-primary btn-lg btn-block" type="submit" id="dlf_submit"  name="dlf_submit" value="Log in" />
        </div>
</form>

<p class="login-link">
<a href="<?php echo site_url('/register'); ?>" title="Register">Register</a>&nbsp;|
<a href="<?php echo wp_lostpassword_url(); ?>" title="Lost your password?">Lost your password?</a>
</p>

<?php
}

function dlf_auth( $username, $password ) {

global $user;
get_currentuserinfo();
$creds = array();
$creds['user_login'] = $username;
$creds['user_password'] =  $password;
$creds['remember'] = true;
$user = wp_signon( $creds, false );


if ( is_wp_error($user) ) {

       //get user email
       $email = $_POST['login_name'];
       $users= get_user_by( 'email', $email );//
       $key = 'user_login_status';
       //get user login status
       $user_status = get_user_meta( $users->ID, $key);
       $user_approve = $user_status[0];
       $error_msg = '';

       //echo $user->get_error_message();exit;

       //if email does not exist in DB
       if(!email_exists( $email ) ){
       $error_msg = '<div style="margin-bottom: 10px" class="btn btn-block btn-md btn-danger"><strong>ERROR: E-mail address does not exist.</strong></div>';       

       //if user is not activated
       }elseif($user_approve == 'inactive'){
       $error_msg = '<div style="margin-bottom: 10px" class="btn btn-block btn-md btn-danger"><strong>ERROR: Your account has to be activated before you can login.</strong></div>';

       //if invalid email or password
       }else{

           //echo $user->get_error_message();exit;
       $error_msg = '<div style="margin-bottom: 10px" class="btn btn-block btn-md btn-danger"><strong>ERROR: Invalid e-mail address or password.</strong></div>';
       }

       echo $error_msg;
    //echo $user->get_error_message();
} 

if ( !is_wp_error($user)) {
    wp_redirect(site_url().'/overview');
    exit();
}

}

function dlf_process() {
if (isset($_POST['dlf_submit'])) {
    dlf_auth($_POST['login_name'], $_POST['login_password']);
}

dlf_form();
}

function dlf_shortcode() {
ob_start();
dlf_process();
return ob_get_clean();
}

add_shortcode('dm_login_form', 'dlf_shortcode');

?>



echo '<pre>';print_r($user);exit; this gives all the user details but user can not login it shows blank Login page. The  same code works on `localhost` but not on my staging server.

On inspecting the page source it halts from below div which is in my theme's page.php

<div class="entry-content">

                        <?php the_content( __( 'Continue reading <span class="meta-nav">...</span>', 'flat' ) ); ?>

                        <?php wp_link_pages( array( 'before' => '<div class="page-links"><span class="page-links-title">' . __( 'Pages:', 'flat' ) . '</span>', 'after' => '</div>', 'link_before' => '<span>', 'link_after' => '</span>' ) ); ?>

                    </div>

In my header.php I have used below code and printing it give me null

<?php $current_user = wp_get_current_user();
print_R($current_user);exit;

?>

WP_User Object ( [data] => [ID] => 0 [caps] => Array ( ) [cap_key] => [roles] => Array ( ) [allcaps] => Array ( ) [filter] => ) 
  • You are using undeinfed variables $username and $password for the $creds array. – cybmeta Feb 20 '15 at 8:07
  • @cybmeta question updated – Slimshadddyyy Feb 20 '15 at 8:34
  • Please reformat your code. It's really, really hard to read that way. You may also show how you are triggering that function because it's unclear of the right data needed to log the user in is used. – kaiser Feb 20 '15 at 9:49
  • @kaiser code updated. Please check – Slimshadddyyy Feb 20 '15 at 9:56
  • Again: You should really, really fix that markup. This is not readable. – kaiser Feb 20 '15 at 11:49
0

I think the wp_signon() function is unable to set the login cookies because you are calling it after the page headers have being sent.

Try moving your auth code out of dlf_process() and into a separate function like this:

function dlf_login() {
    if ( isset( $_POST['dlf_submit'] ) ) {
        dlf_auth( $_POST['login_name'], $_POST['login_password'] );
    }
}
add_action( 'after_setup_theme', 'dlf_login' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.