skip to Main Content

i’m working on customisation of a simple login page of wordpress.

the actual problem is even when i give the correct data for login it won’t login and has an error

here are my try :

<?php
/** Template Name: login */
 global $user_ID;

if (!$user_ID){
    if ($_POST){
    $username= esc_sql($_POST['username']);
    $password= esc_sql($_POST['password']);
    $login_array= array();
    $login_array['username']=$username;
    $login_array['password']=$password;
    $verify_user=wp_signon($login_array,true);
    if(!is_wp_error($verify_user)){
    echo "<script>window.location='".site_url()."'</script>";
    }

    else{


        ?>
        <form action="" method="post">
            <input type="text" name="username" id="username" placeholder="نام کاربری">
            <input type="password" name="password" id="password" placeholder="رمز عبور">
            <button type="submit">ورود کارکنان</button>
        </form>
        <?php
    }
    }
}

everything seems be correct but something is wrong
could someone please help me ?

2

Answers


  1. regardless of the problem you are facing
    Why not use the Ajax login and terminate the problem

    https://gist.github.com/cristianstan/10273612

    Login or Signup to reply.
  2. There’s a few things you probably need to do.

    1. This has to happen BEFORE headers are sent.
    2. Rather than use global $user_ID, use is_user_logged_in()

    This would work:

    global $wpdb;
    if ( !is_user_logged_in() ) {
        if ( isset( $_POST['username'] ) ) {
            $username = $wpdb->_escape( $_POST['username'] );
            $password = $wpdb->_escape( $_POST['password'] );
            $login_array = array();
            $login_array['user_login'] = $username;
            $login_array['user_password'] = $password;
            $verify_user = wp_signon( $login_array, is_ssl() );
            if ( !is_wp_error( $verify_user ) ) {
                wp_redirect( site_url() );
            }
        } else {
    
            get_header();
            ?>
            <form action="" method="post">
                <input type="text" name="username" id="username" placeholder="نام کاربری">
                <input type="password" name="password" id="password" placeholder="رمز عبور">
                <button type="submit">ورود کارکنان</button>
            </form>
            <?php
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search