0

I would like to create the following functionality: A Wordpress website should be available ONLY to logged in users.

  • If user is not logged in, he must be redirected to a custom page (currently created as a page-template)
  • When user logs in, he will be redirected to the homepage
  • No page or post should be available to logged out users

Worth to mention: I am using Wordpress social login for users to login, not standard WordPress login

1

WordPress has a function to check if user is logged in or not.

is_user_logged_in()

You can use this function with conditional statements to restrict not logged in users from accessing contents.

for example if you want to restrict the homepage for not logged in users you may do this with following code.

function annframe_restrict_access() {

 if( is_home() || is_front_page() ) {
   if( ! is_user_logged_in() ) {
         wp_redirect( home_url( 'page-name' ) );
         exit();
   }
 }

}

add_action( 'init', 'annframe_restrict_access' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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