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

FrenchyNYC picture FrenchyNYC · Dec 9, 2015 · Viewed 9.7k times · Source

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.

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 !

Answer

vvamondes picture vvamondes · Dec 9, 2015

Take a look here

https://wordpress.org/support/topic/how-to-auth_redirect-to-specific-page

<?php
if( !is_user_logged_in() ) {
    wp_redirect('http://somepagehere');
    exit;
}
?>

You can also add this to your functions.php file instead of what I said before:

function admin_redirect() {
if ( !is_user_logged_in()) {
   wp_redirect( home_url('/{custom page goes here}/') );
   exit;
}
}
add_action('get_header', 'admin_redirect');