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

痞子三分冷 提交于 2019-12-08 04:32:14

问题


I want to close a wordpress blog I have from the public and keep it only for myself.

I know I can set it to private but it shows an uggly log-in page and I dont want people trying to access it (using random usernames/pass etc) or think that its still open but its for members with accounts or anything like that.

I would like the blog to point to a " server not found " for the public and when I am logged in as admin to be able to see my posts and backend aswell as frontend.

How can I make this possible?


回答1:


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




回答2:


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
}


来源:https://stackoverflow.com/questions/22519805/how-to-make-wordpress-blog-completely-invisible-to-public-not-private

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!