1

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?

2 Answers 2

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

7
  • Thank you for the answer NAthan, where should I put this in header.php index.php ? I guess the approach to this is to make it private blog and use the above code? When someone isnt logged in would it give him a 404 page like when a site doesnt exist or the "inner 404" using the template of the website and displaying 404? Mar 19, 2014 at 23:07
  • Put it in functions.php. Yep that's exactly what it does. Mar 19, 2014 at 23:11
  • An odd thing has happened, I tried to find the "I would like my blog to be visible only to users I choose" in Reading options in wordpress before I use the code above and its not there. Any idea what I could do? (I am using latest version of wordpress 3.8.1) Mar 19, 2014 at 23:28
  • That's the first time I've heard of such an option. Mar 19, 2014 at 23:38
  • I cant find a way to make it private, usually I could go in "Reading" => Making this blog private (something like this under the block search engines option) Mar 19, 2014 at 23:40
0

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
}
2
  • Should I place this in the header.php or in the index.php? I want the whole site to go "server not found" if im not logged in. Mar 19, 2014 at 22:53
  • This should wrap around your content, whatever you'd like to show / hide.
    – benomatis
    Mar 19, 2014 at 22:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.