在Wordpress登录小部件中显示消息"wrong user"



我正在搜索如何在此处找到的自定义小部件中显示"错误的用户"消息,但没有成功:

无需密码即可将用户登录WordPress

下面是函数中添加的代码.php

<?php
class No_Password_Widget extends WP_Widget 
{
    public function __construct() {
        parent::__construct("no-password-widget", "No Password Login Form", array("description" => __("Displays a Login form which allows to login without password")));
        }
    public function widget($args, $instance) 
    {
        if(is_user_logged_in())
        {
            echo "You are logged in";
        }
        else
        {
            ?>
                <form method="get" action="<?php echo get_site_url() . '/wp-admin/admin-ajax.php'; ?>">
                    <input type="text" name="username" placeholder="Username" required>
                    <input type="text" name="action" value="login" style="display: none">
                    <input type="submit">
                </form>
            <?php
        }
    }
}
register_widget("No_Password_Widget");
function redirect_to_home()
{
    header("Location: " . get_site_url());
}
function login()
{
    if(isset($_GET["username"]))
    {
        //now login user 
        $user = get_user_by("login", $_GET["username"] );
        if($user != "FALSE")
        {
            wp_set_auth_cookie($user->ID);
        }
    }
    header("Location: " . $_SERVER["HTTP_REFERER"]);
    die();
}
add_action("wp_ajax_login", "redirect_to_home");
add_action("wp_ajax_nopriv_login", "login");

但是没有办法用这段代码做到这一点。

该小部件运行良好,但没有错误的用户消息。

请帮助我。

提前谢谢。

你试过吗?

这是您提供的链接的一部分,如果这不起作用,我会进一步研究它。

  if($user != "FALSE")
    {
        wp_set_auth_cookie($user->ID);
    }
    else
    {
        return null;
    }
    return $user;

相关内容

最新更新