Question

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?

Was it helpful?

Solution

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

OTHER TIPS

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
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top