drfif48428
2019-02-04 16:28
浏览 80
已采纳

Wordpress:程序化登录无效。 此外,wp_redirect无效。 不知道为什么

[ Please forget about security concerns for a moment... ]

My goal is to create an invitation page where people land on after being invited by an email with a unique link. On that page they choose a password using a basic HTML form, which posts into the very same page.

When posted, my goal is to programmatically log the user in (the backend knows the email + the password based on the unique invite code) and redirect that user to another page, as a logged in user.

my code has NO EFFECT. The user is not logged in (I can see that by refreshing another page in my web app in another browser tab, where the header differs if the user is logged in). Also-- the redirect has no effect. We never navigate out of the page we posted to.

Here's the code. It is being executed in the POST portion of the page:

 $creds = array(
        'user_login'    => $user->user_email,  // VERIFIED- CORRECT
        'user_password' => $password,          // VERIFIED- CORRECT
        'remember'      => true
    );

    $user = wp_signon( $creds, false );

    if ( is_wp_error( $user ) ) {
        echo $user->get_error_message(); // DOESN'T GET HERE
    }

    wp_redirect('http://mywebapp.loc/faq'); // DOESN'T REDIRECT
    die();

I would appreciate any pointer. Obviously I am doing something wrong.

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongli4711 2019-02-04 16:52
    已采纳

    I was calling wp_signon and wp_redirect after the headers have been sent, and as redirect + session cookies are sent in those headers -- it was too late. Moving that piece of code BEFORE the headers resolved the issue.

    for me it was as simple as moving this code down the php file, until after the wp_signon and wp_redirect section:

    <?php
     get_header();
    ?>
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报