How to make wordpress blog completely invisible to public [Not Private]

末鹿安然 提交于 2019-12-08 21:40:47

My suggestion would be the following:

function is_login_page() {
    return in_array( $GLOBALS['pagenow'], array( 'wp-login.php', 'wp-register.php' ) );
}

function wpse_make_blog_private() {
    if ( ! is_user_logged_in() && ! is_admin() && ! is_login_page() ) { 
        global $wp_query;
        $wp_query->set_404();
    }
}
add_action( 'wp', 'wpse_make_blog_private' );

It will show a 404 on all pages but still allow you to login. Logged in users will see the site as normal.

On your request to show a completely broken site add the following code to functions.php. Be aware that this is instead of the code above.

function is_login_page() {
    return in_array( $GLOBALS['pagenow'], array( 'wp-login.php', 'wp-register.php' ) );
}

function wpse_make_blog_private() {
    if ( ! is_user_logged_in() && ! is_admin() && ! is_login_page() ) { 
        die();
    }
}
add_action( 'wp', 'wpse_make_blog_private' );

You have the option of using wp_die instead to add an error message with minimal styling. See: https://codex.wordpress.org/Function_Reference/wp_die

I suggest you wrap all the code you want to be visible to logged in users inside the following check:

if ( is_user_logged_in() ) { 
    // do stuff for logged in users
} else {
    // do stuff for guests / users that are not logged in
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!