Wordpress WPMU跨多站点网络的登录一致性


Wordpress WPMU Login consistency across multisite network

我正在进行WPMU多站点安装,但遇到了一个小问题。

我在主域的注册过程中创建了一个用户。用下面这样的东西。

$username = 'myname-'.time();
$user_id = wpmu_create_user($username,'anypassword','example@gmail.com');
add_user_to_blog(1, 5, 'subscriber');
$user = wp_signon(array(
"user_login" => $username,
"user_password" => 'anypassword',
"remember" => true
));

我所做的是创建用户,然后仅将其分配给主域,并使用wp_signon将用户登录。然而,当访问子域上的网络子网站时,重要的是它的访问非常受限。我仍然登录,顶部的仪表板菜单仍然显示。

我使用is_user_blog()来尝试确定用户是否应该能够看到它,并将其引导到子域的登录页面。但这意味着终止主域上现有的登录会话。理想情况下,如果您可以登录到主域,也可以登录到子域,但两者都单独处理,那将是很酷的。

以前有人遇到过这个问题吗?

是的,我遇到了这个问题。而且,如果你需要特殊的用户管理,你必须设置一个新的自主(单站点)WordPress安装。

这就是Multisite的工作方式。所有用户被自动包括为网络中所有站点的subscribers

来自文章不要使用WordPress多站点:

如果你需要用户在不同的网站上,但不知道他们在网络上,不要使用MultiSite!现在,是的,有办法解决这个问题,但这对任何大公司来说都是一场审计噩梦,也是一个安全风险,你应该在开始之前意识到。

这个插件可能有帮助(但我不确定):多站点用户管理。

从我最近在WordPress StackExchange上给出的答案来看,一些小技巧可能会有所帮助:
(我在开发环境中做了一些小测试,但请广泛测试)

/*
 * Redirect users that are not members of the current blog to the home page, 
 * if they try to access the profile page or dashboard 
 * (which they could, as they have subscriber privileges)
 * http://not-my-blog.example.com/wp-admin/profile.php
 */
add_action( 'admin_init', 'wpse_57206_admin_init' );
function wpse_57206_admin_init()
{
    if( !is_user_member_of_blog() ) 
    {
        wp_redirect( home_url() );
        exit();
    }
}

/*
 * Redirect users that are not members of the current blog to the home page, 
 * if they try to access the admin
 * http://not-my-blog.example.com/wp-admin/
 */
add_action( 'admin_page_access_denied', 'wpse_57206_access_denied' );
function wpse_57206_access_denied()
{
    wp_redirect( home_url() );
    exit();
}

/*
 * Redirect users that are not members of the current blog to the home page, 
 * if they try to login
 * http://not-my-blog.example.com/wp-login.php
 */
add_filter( 'login_redirect', 'wpse_57206_login_redirect' );
function wpse_57206_login_redirect( $url )
{
    global $user;
    if ( !is_user_member_of_blog() ) 
    {
        $url = home_url();
    }
    return $url;
}

/*
 * Hide the admin bar for users which are not members of the blog
 */
add_filter( 'show_admin_bar', 'wpse51831_hide_admin_bar' );
function wpse51831_hide_admin_bar( $bool )
{
    if( !is_user_member_of_blog() )
    {
        $bool = false;
    }
    return $bool;
}