您可以使用功能强大的插件,如Multisite Privacy by WPMUDEV, 或者您可以使用我为自己的项目开发的以下代码。
要使用此代码,可以使用它创建一个插件,您只能在站点上激活该插件以进行保护,或者如果这听起来太困难,您可以将其粘贴到主题的功能中。php并使用条件语句包装add\\u操作调用,如下所示:
if ( get_current_blog_id() = 2 ) // Only apply privacy to blog ID 2
add_action(\'wp\', \'private_site\');
function private_site() {
if( $_GET[\'home\'] == \'fix\' ) { return; }
$isLoginPage = strpos($_SERVER[\'REQUEST_URI\'], "wp-login.php") !== false;
$isPhoneApp = strpos($_SERVER[\'REQUEST_URI\'], "xmlrpc.php") !== false;
$wpe_cookie = \'wpe-auth\';
if( !is_user_logged_in() && !is_admin() && !$isLoginPage && !$isPhoneApp ) {
// WPE: If not-authenticated, delete our cookie in case it exists.
if ( isset($_COOKIE[$wpe_cookie]) ) setcookie($wpe_cookie,\'\',time()-1000000,\'/\');
$shareKey = get_post_meta( get_the_ID(), \'key\', true );
if( $_GET[\'key\'] && is_single && $_GET[\'key\'] == $shareKey ) {
return;
} else {
$location = get_login_redirect_url( get_bloginfo( \'url\') . $_SERVER[\'REQUEST_URI\'] );
header( \'Location: \' . $location );
exit();
}
} else {
// WPE: Authenticated, so set the cookie properly. No need if it\'s already set properly.
$cookie_value = md5(\'wpe_auth_salty_dog|\'.WPE_APIKEY);
if ( ! isset( $_COOKIE[$wpe_cookie] ) || $_COOKIE[$wpe_cookie] != $cookie_value )
setcookie($wpe_cookie,$cookie_value,0,\'/\');
}
}
// Returns the login URL with a redirect link.
function get_login_redirect_url( $url = \'\' ) {
$url = esc_url_raw( $url );
if ( empty( $url ) ) return false;
// setup query args
$query_args = array(
\'redirect_to\' => urlencode( $url )
);
return add_query_arg( $query_args, apply_filters( \'ass_login_url\', wp_login_url() ) );
}