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();
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献