1

I'm trying to make a login form, but select query isn't working. Is there another syntax in WordPress and in which way is password hashed? My code is:

if ( isset( $_POST['submit'] ) ) {
    if ( !$_POST['username']  ) {
        echo "You did not complete all of the required fields";
    } else { 
        $username=$_POST['username'];
        $password=$_POST['password'];
        $query = $wpdb->get_row( "SELECT * FROM $wpdb->wp_users
            WHERE user_login='@admin' && user_pass = 'some_pass'" );
        if ( $query != null ) {
            echo "Login success";
            return true;
        } else {
            echo "incorrect pass or user";
            return false;
        }
    }
} 
?>
<form action="" method="post"/>
    <input type="text" name="username" placeholder="Username"/> 
    <input type="text" name="password" placeholder="Password"/><br> 
    <input type="submit" name="submit" value="Submit">
</form>

2 Answers 2

1

You can use wp_signon function

Here is an example

    $creds = array();
    $creds['user_login'] = 'example';
    $creds['user_password'] = 'plaintextpw';
    $creds['remember'] = true;
    $user = wp_signon( $creds, false );
    if ( is_wp_error($user) )
        echo $user->get_error_message();
1
  • It gives me error: The password field is empty. I used: $username=$_POST['username']; $password=$_POST['password']; $creds = array(); $creds['user_login'] =$username ; $creds['user_pass'] = $password; $creds['remember'] = true; $user = wp_signon( $creds, false ); if ( is_wp_error($user) ) echo $user->get_error_message(); <form action="" method="post"/> <input type="text" name="username" placeholder="Username"/> <input type="text" name="password" placeholder="Password"/><br> <input type="submit" name="submit" value="Submit"> </form>
    – ci_lover
    Commented Jun 19, 2015 at 19:50
1

you need to just put it in to variable and that variable call in to query.

  $table_name = $wpdb->prefix . "sign_up";
        $query = $wpdb->get_row( "SELECT * FROM $table_name
            WHERE email_id='".$username."' && password = '".$password."'" );

Your Answer

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

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