0

I am using the front end form submission. It works on localhost perfectly, but it's not working when my site is live. It shows the "success message", but originally it's not logged in. This is my code:

if ( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) && $_POST['action'] == 'log-in' ) :    
    global $error;
    $login = wp_login( $_POST['user-name'], $_POST['password'] );
    $login = wp_signon( array( 'user_login' => $_POST['user-name'], 'user_password' => $_POST['password'], 'remember' => $_POST['remember-me'] ), false );

    if (!$error) {
        save_message( 'success', __( 'You have successfully Login.', 'frontendprofile' ) );
        unset($_POST);
        wp_redirect( get_permalink( 4 ) );  
    }
    // wp_redirect( home_url() );
endif;

So what can I do to fix this?

2 Answers 2

0

Do not use wp_login function it is deprecated , instead use wp_signon. wp_signon function returns WP_Error on failure so use it to evaluate condition as following.

<?php
if ( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) &&  $_POST['action'] == 'log-in' ) :

$login = wp_signon( array( 'user_login' => $_POST['user-name'], 'user_password' => $_POST['password'], 'remember' => $_POST['remember-me'] ), false );

if ( !is_wp_error($login) ){ 
save_message( 'success', __( 'You have successfully Login.', 'frontendprofile' ) );
unset($_POST);
wp_redirect( get_permalink( 4 ) );
}

endif;
?>
5
  • Your code is still not working having same problem.
    – Adi
    Mar 15, 2013 at 11:43
  • Is it displaying success message? Mar 15, 2013 at 12:01
  • Yes it displaying success message but not logged in
    – Adi
    Mar 15, 2013 at 12:34
  • Can you just show me your whole code of registration model including from where you are passing user-name, password etc. Mar 15, 2013 at 13:11
  • Sory for the late reply. Yes i can email you the file with all code. just provide me you email i will email you. Thanks
    – Adi
    Mar 18, 2013 at 8:39
0

You need to set cookie, I modified your code as below:

Updated

<?php
function parse_user($info = null, $return = 'object') {
    if ( is_null( $info ) ) {
        global $current_user;
        if ( empty( $current_user->ID ) ) return null;
        $info = get_userdata( $current_user->ID );
    }
    elseif ( empty( $info ) ) {
        return null;
    }
    if( $return == 'ID' ) {
        if ( is_object( $info ) ) return $info->ID;
        if ( is_numeric( $info ) ) return $info;
    }
    elseif( $return == 'object' ) {
        if ( is_object( $info ) && $info->ID) return $info;
        if ( is_object( $info )) return get_userdata( $info->ID );
        if ( is_numeric( $info ) ) return get_userdata( $info );
        if ( is_string( $info ) ) return get_userdatabylogin( $info );
    }   
    else {
        return null;
    }
}

function wp_login_user_po9856($username, $password, $remember) {
    // Get the user based on the username from the POST
    $user = parse_user($username);

    // Remove html tags from the variables
    $username = strip_tags($username);
    $password = strip_tags($password);

    // Validate the Form Data
    if(!wp_check_password( $password_stripped, $user->user_pass ) ) return new WP_Error('incorrect_password', "Wrong Password!");

    // User login
    $login = wp_signon(array('user_login' => $username, 'user_password' => $password, 'remember' => (bool)$remember));
    if (is_wp_error($login))
        return $login;

    // Unset
    unset($_POST);

    // Custom Message
    save_message( 'success', __( 'You have successfully Login.', 'frontendprofile' ) );

    // Redirection
    wp_redirect(get_permalink( 4 ));
}
if ( 'POST' == $_SERVER['REQUEST_METHOD'] && @$_POST['action'] == 'log-in')
{
    wp_login_user_po9856($_POST['user-name'], $_POST['password'], $_POST['remember-me']);
}

?>

12
  • It is not mandatory to set cookie, the registration can work without setting secure_cookie parameter for the wp_signon function. Mar 15, 2013 at 11:38
  • Your code stuck the page it display nothing. Not working properly.
    – Adi
    Mar 15, 2013 at 11:44
  • @VinodDalvi it's not registration, it's about login, however WP DOC stated This function sets an authentication cookie. Users will not be logged in if it is not sent, using that function also makes sense.
    – revo
    Mar 15, 2013 at 12:02
  • @Adi are u sure u removed your code completely and then paste mine ?
    – revo
    Mar 15, 2013 at 12:03
  • @rev yes i completely remove my code and paste your code.
    – Adi
    Mar 15, 2013 at 12:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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