How to redirect non logged in users to Login page on WordPress?

I know this is a question asked million times but I can’t find an answer that is either fitting the version of WordPress now and/or my particular case.

I have a WordPress website with Buddypress installed on it.

Read More

I want that :
– Not logged in users can see only the page wp-login.php
– Logged in users can access the whole website

I just can’t figure it out, it is impossible. Either I change the site address or assign a page, or fuctions… Nothing works, all the time an error or a 404 page error etc…

Basically : you arrive on the website, you are not logged in = Wp-Login.php, if you are logged in page “members”

Thank you !

Related posts

4 comments

  1. There are a lot of different ways to do this based on what your ultimate goal is (use WP login page, a custom login page, etc…). You can try adding this to your theme’s functions.php file:

    if ( ( is_single() || is_front_page() || is_page() || is_archive() || is_tax() )
        && ! is_page( 'login' ) && ! is_page('register') && ! is_user_logged_in() ) {
        auth_redirect(); 
    }
    

    Or you can use the plugin, Force Login

    UPDATE

    Theoretically, you can probably just use this, just haven’t tested…

    if( ! is_page('login') && ! is_page('register') && ! is_user_logged_in() ) {
        auth_redirect(); 
    }
    
  2. What you want it to check if the user is not currently trying to login or register, if not redirect them to whatever page you’d like.

    if ( !is_user_logged_in() && !is_page( 'login' ) && ! is_page('register') ) {
       //redirect user, create account with them, do a hoola-hoop
    }
    

    Be sure to place this code in your page file before get_header();

Comments are closed.