2 comments

  1. finally this is working for me on my local WP installation after replacing – with _ from input attribute name and using full php start tags <?php instead of <? the final code is here copy and paste into your template.

    <?php if ( isset($_POST["user_email"]) && isset($_POST["user_password"]) ) {
    
    $user_login     = esc_attr($_POST["user_email"]);
    $user_password  = esc_attr($_POST["user_password"]);
    $user_email     = esc_attr($_POST["user_email"]);
    
    $user_data = array(
        'user_login'    =>      $user_login,
        'user_pass'     =>      $user_password,
        'user_email'    =>      $user_email,
        'role'          =>      'student'
    );
    
    // Inserting new user to the db
    //wp_insert_user( $user_data );
    
    $creds = array();
    $creds['user_login'] = $user_login;
    $creds['user_password'] = $user_password;
    $creds['remember'] = true;
    
    $user = wp_signon( $creds, false );
    
    $userID = $user->ID;
    
    wp_set_current_user( $userID, $user_login );
    wp_set_auth_cookie( $userID, true, false );
    do_action( 'wp_login', $user_login );
    
    }
    
    if ( is_user_logged_in() ) : echo 'SUCCESS'; ?>
    <h1>Html for logged in user </h1>
    <?php else : echo 'FAIL!'; ?>
    <form id="user-credentials" method="post" action="<?php the_permalink(); ?>">
        <p><input name="user_email" type="text" placeholder="Email" /></p>
        <p><input name="user_password" type="password" placeholder="Password" /></p>
        <p><input type="submit" class="button blue size-s" value="Submit" /></p>
    </form>
    <?php endif; ?>
    
  2. If your password includes the # and/or @ characters, you need to urlencode before sending in the form, and then urldecode before setting the credentials in wp_signon.

    The url encoding for @ is %40, # is %23 and ! is %21

    if your password is mY!p@ssWord# then should be URL encoded before POSTING to mY%21p%40ssWord%23, then URL decoded before setting the credentials for wp_signon

Comments are closed.