doujiu7704 2016-02-23 07:10
浏览 43
已采纳

而使用wp_signon($ creds,false)得到错误

I want to login user automatically when user is activated in multisite. but when I am trying to use wp_signon() it response with the error

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\krosswall\wp-includes\class.wp-styles.php:127) in C:\xampp\htdocs\krosswall\wp-includes\pluggable.php on line 955

I am trying to do this

function kw_activate_blogs( $user_id, $password, $meta ) {

    add_user_to_blog( '2', $user_id, get_site_option( 'default_user_role', 'subscriber' ) );

    $user = new WP_User( (int) $user_id );
    $creds = array();
    $creds['user_login'] = $user->user_login;
    $creds['user_password'] = $password;
    $creds['remember'] = true;
    $user = wp_signon( $creds, false );
    wp_set_current_user($user->ID);
    if ( is_wp_error($user) ) {
        echo $user->get_error_message();
    } else {
    // safe redirect to actually login the user - otherwise they would need to manually refresh the page
    // PLUS: this clears the activation confirmation page with the plain text password printed on screen
    //wp_safe_redirect( get_home_url() );
        exit;
    }

}

add_action( 'wpmu_activate_user', 'kw_activate_blogs', 10, 3 );    

I added this code in functions.php file. Please help me to solve it.

  • 写回答

1条回答 默认 最新

  • dpjhq00684 2016-02-23 07:21
    关注

    You can do this:

    function kw_activate_blogs($user_id, $password, $meta)
    {
        $result=add_user_to_blog('2', $user_id, get_site_option('default_user_role', 'subscriber'));
        if (!is_wp_error($result))
        {
            wp_set_current_user($user_id);
            if (wp_validate_auth_cookie() == FALSE)
            {
                wp_set_auth_cookie($user_id, true, false);
            }
        }
        else
        {
        //do something here on error
        }
    }
    

    You don't need their password in this case. You can also remove 2nd and 3rd arguments if you don't need $meta and change add_action to add_action( 'wpmu_activate_user', 'kw_activate_blogs', 10);

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

报告相同问题?

悬赏问题

  • ¥15 Oracle触发器记录修改前后的字段值
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器