把这个放在一个盒子里。php文件,将其作为插件上传,然后通过网络激活它。有关详细信息,请阅读代码中的注释。
如果您有任何疑问或它对您不起作用,请告诉我。我这样做是为了让超级管理员可以使用所有网站,无论发生什么。
<?php
/*
Plugin Name: No Role No Login
Description: If the user in a multisite has no role for a site, he will not be able to access it while logged in. Does not affect super administrators.
Author: Nikolay Nikolov
Version: 1.0.0
*/
add_action( \'init\', \'no_role_no_login\' );
function no_role_no_login() {
// If the user is logged-in and not a super administrator we continue
if ( is_user_logged_in() && ! current_user_can( \'manage_network\' ) ) {
// If this is a logout attempt, we do nothing, so we allow it
if ( ( ( isset( $GLOBALS[\'pagenow\'] ) && \'wp-login.php\' === $GLOBALS[\'pagenow\'] )
|| ( isset( $_SERVER[\'PHP_SELF\'] ) && \'/wp-login.php\' === $_SERVER[\'PHP_SELF\'] ) )
&& isset( $_GET[\'action\'] ) && \'logout\' === $_GET[\'action\'] ) {
// If this is not a logout attempt, we will check if the user has a role for the site and deny access if he does not (and show a message)
} else {
$get_users = get_users(
array(
\'blog_id\' => get_current_blog_id(),
\'include\' => array( get_current_user_id() )
)
);
if ( empty( $get_users ) ) {
wp_die( \'You cannot access this site while logged in. To view it, you have to <a href="\' . wp_logout_url() . \'">logout</a>.\' );
}
}
}
}