构建自定义登录表单-遇到问题

时间:2017-11-21 作者:AKor

我正在构建一个自定义登录表单,并尝试了两种方法:wp_authenticate()wp_signon().

对于前者,我能够正确地返回WP_User 对象,但没有设置Cookie来向其发送信号is_user_logged_in() 用户确实已登录。我想我可以自己做饼干,但有没有更简单的方法?

对于后者,我一直有错误Cannot modify header information - headers already sent by.... 我试着打电话给wp_signon() 来自的函数<head>, 实际页眉,以及页面模板中的。不管怎样,我都看到了相同的错误。我在网上查过,但没有找到解决这个问题的方法。

具体错误指向的是base.php - Warning: Cannot modify header information - headers already sent by (output started at /srv/www/mysite/current/web/wp/wp-content/themes/mysite/base.php:7).

但从这里看,我没有发现任何问题(这些是第4-10行,第7行是doctype):

use Roots\\Sage\\Wrapper;

?>
<!doctype html>
<html <?php language_attributes(); ?>>
  <?php get_template_part(\'templates/head\'); ?>
  <body <?php body_class(); ?>>
根据WP自己的故障排除,我已经检查过没有多余的空间。

无论它是如何进行的,我只需要能够使用用户的凭据登录,并且能够检查他们的登录状态,以便在网站上创建登录状态。

有什么帮助吗?

1 个回复
SO网友:Charlie Stanard

看看authenticate filter. 这会帮你处理一切。

function wpse_custom_login($user, $username, $password) {
    if (empty($username)) {
        $errors = new WP_Error();
        //add some error message, return $errors
    }
    $user = get_user_by(\'email\', $username);
    // do some additional validation, checks, etc.
    return $user;
}
add_filter(\'authenticate\', \'wpse_custom_login\', 30, 3);
这是一个非常简单的示例,但开始演示如何通过电子邮件地址而不是用户名登录用户。

结束

相关推荐

是否使用wp_LOGIN_FORM函数使用自定义用户表登录?

是否可以使用WP内置功能wp_login_form 在自定义非wp用户表上??如果我可以在非wp用户表中作为参数,那就太棒了$args = array( \'redirect\' => home_url(), \'id_username\' => \'user\', \'id_password\' => \'pass\', \'table_user\' =>mycustom_table )