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

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:

    本回答被题主选为最佳回答 , 对您是否有帮助呢?



  • ¥15 关于#Mybatis#的问题:怎么办重复数据如下"KSDM":37,"KSMC":"呼吸内科门诊","ksdm":37,"ksmc":"呼吸内科门诊"xml文件填写
  • ¥40 servlet的web程序部署出错
  • ¥50 activiti 新建流程系列问题
  • ¥50 为什么我版本升级之后运行速度变慢5倍??
  • ¥15 如何在gazebo中加载机械臂和机械手
  • ¥15 纯运放实现隔离采样方案设计
  • ¥20 easyconnect无法连接后缀带.com的网站,一直显示找不到服务器ip地址,但是带有数字的网站又是可以连接上的,如何解决
  • ¥15 电脑开机过商标后就直接这样,求解各位
  • ¥15 mysql , 用自己创建的本地主机和用户名 登录不上
  • ¥15 关于#web项目#的问题,请各位专家解答!