为某些定义的用户角色创建WordPress用户登录系统的程序

时间:2018-09-24 10:56:28

标签: wordpress login

我已经更新了代码,但是问题是get_the_ID没有得到任何值。你能帮忙吗?这段代码在子主题中添加了function.php。

// Works in single post outside of the Loop
add_filter( 'authenticate', 'myplugin_auth_signon', 30, 3 );
function myplugin_auth_signon( $user, $username, $password ) {
        $user       =   get_user_by( 'login', $username );
        $roles      =   $user->roles['0'];

            $id = get_the_ID();
            echo $id;
            if ( is_page( $id == 400380 ) )
            {
                echo "Employee Page";
                $user = new WP_Error( 'denied', "Customer have no permission to login from this employee login form" );


            }
            if ( is_page( $id == 399649 ) )
            {
                echo "Customer";

            $user = new WP_Error( 'denied', "Employee have no permission to login from this customer login form" );


            }



    return $user;

}

2 个答案:

答案 0 :(得分:1)

您可以使用登录authenticate过滤器挂钩来限制其他角色。

add_filter( 'authenticate', 'myplugin_auth_signon', 30, 3 );

function myplugin_auth_signon( $user, $username, $password ) {
        $user       =   get_user_by( 'login', $username );
        $roles      =   $user->roles['0'];

        if($roles != 'sales' && is_page('YOUR_SALES_PAGE_ID')){
            $user = new WP_Error( 'denied', "You have not permission to login from this form" );
            return $user;
         }
    return $user;
}

用您当前的销售登录页面ID替换YOUR_SALES_PAGE_ID

有关更多帮助,请参见以下链接:Click here

答案 1 :(得分:0)

用户角色来自database吗?先检查一下,然后再做逻辑。