login - wp_signon() does not authenticate user guidance needed

I use form to send POST request to a page and to login user with wp_signon() in order to authenticate user to my wordpre

I use form to send POST request to a page and to login user with wp_signon() in order to authenticate user to my wordpress installation as described in WP documentation:

$creds = array();
$creds['user_login'] = $_POST["user-login"];
$creds['user_password'] = $_POST["user-password"];
$creds['remember'] = true;

$user = wp_signon( $creds, false );

After this little piece of code I'm checking if user was logged in:

if ( is_user_logged_in() ) { echo "SUCCESS"; } else { echo "FAIL!"; }

But I got FAIL! all the time. Then after sniffing around I found this little trick:

wp_set_current_user( $user );
if ( is_user_logged_in() ) { echo "SUCCESS"; } else { echo "FAIL!"; }

I've got SUCCESS on this one but when I leave this page I got FAIL again and again.

Can anyone explain me how to login user with wp_signon() without logging her out after page is changed or reloaded or whatever.


I've got desirable result when I go to /wp_admin and login with WP's default login form. I can navigate through every page of my WP site remaining logged-in all the time. But when I try to do this outside the default form I FAIL.

Guide me! PLEASE!

I use form to send POST request to a page and to login user with wp_signon() in order to authenticate user to my wordpress installation as described in WP documentation:

$creds = array();
$creds['user_login'] = $_POST["user-login"];
$creds['user_password'] = $_POST["user-password"];
$creds['remember'] = true;

$user = wp_signon( $creds, false );

After this little piece of code I'm checking if user was logged in:

if ( is_user_logged_in() ) { echo "SUCCESS"; } else { echo "FAIL!"; }

But I got FAIL! all the time. Then after sniffing around I found this little trick:

wp_set_current_user( $user );
if ( is_user_logged_in() ) { echo "SUCCESS"; } else { echo "FAIL!"; }

I've got SUCCESS on this one but when I leave this page I got FAIL again and again.

Can anyone explain me how to login user with wp_signon() without logging her out after page is changed or reloaded or whatever.


I've got desirable result when I go to /wp_admin and login with WP's default login form. I can navigate through every page of my WP site remaining logged-in all the time. But when I try to do this outside the default form I FAIL.

Guide me! PLEASE!

Share Improve this question edited Sep 24, 2013 at 18:10 LoomyBear asked Sep 24, 2013 at 17:40 LoomyBearLoomyBear 2051 gold badge3 silver badges9 bronze badges 11
  • is your site have SSL, i mean your site's url scheme http or https ?? – Anjum Commented Sep 24, 2013 at 18:26
  • @Anjum actually I store it on my localhost with MAMP ... so my answer is I don't know )) – LoomyBear Commented Sep 24, 2013 at 18:44
  • can you tell me how do you access your login page i mean your login page url, http://localhost/login-page/ or https://localhost/login-page/ ? – Anjum Commented Sep 24, 2013 at 18:47
  • please post your custom login form code here, so .. – Anjum Commented Sep 24, 2013 at 18:49
  • @Anjum I use just http://localhost:8888/registration/ but if you try to hint on wp_signon( $user, secure:true/false ) I've tried both and it didn't work. Anyway thanks. – LoomyBear Commented Sep 24, 2013 at 18:54
 |  Show 6 more comments

2 Answers 2

Reset to default 8

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; ?>

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745386092a4625446.html

相关推荐

  • login - wp_signon() does not authenticate user guidance needed

    I use form to send POST request to a page and to login user with wp_signon() in order to authenticate user to my wordpre

    4小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信