Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am using the front end form submission. It work on localhost perfectly but when i live my site it not working it show the success message but originally it not logged in. This is my code

<?php
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 now i do.

share|improve this question
add comment

2 Answers

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;
?>
share|improve this answer
    
Your code is still not working having same problem. –  Adi Mar 15 '13 at 11:43
    
Is it displaying success message? –  Vinod Dalvi Mar 15 '13 at 12:01
    
Yes it displaying success message but not logged in –  Adi Mar 15 '13 at 12:34
    
Can you just show me your whole code of registration model including from where you are passing user-name, password etc. –  Vinod Dalvi Mar 15 '13 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 '13 at 8:39
add comment

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']);
}

?>

share|improve this answer
    
It is not mandatory to set cookie, the registration can work without setting secure_cookie parameter for the wp_signon function. –  Vinod Dalvi Mar 15 '13 at 11:38
    
Your code stuck the page it display nothing. Not working properly. –  Adi Mar 15 '13 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 '13 at 12:02
    
@Adi are u sure u removed your code completely and then paste mine ? –  revo Mar 15 '13 at 12:03
    
@rev yes i completely remove my code and paste your code. –  Adi Mar 15 '13 at 12:05
show 7 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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