显示消息"错误的用户"在Wordpress登录小部件中

时间:2018-04-18 15:58:37

标签: wordpress login widget

我没有找到如何显示"错误的用户"我在这里找到的自定义小部件中的消息:

Login User into WordPress without Password

这是代码,添加在functions.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");

但是这段代码无法做到这一点。

小部件工作正常,但没有错误的用户消息。

请帮帮我。

提前致谢。

1 个答案:

答案 0 :(得分:0)

你试过吗?

这是您提供的链接的一部分,如果这不起作用,我会更多地了解它。

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