0

So I have a page (ex. site.com/start) where I want to show a login page (ex. site.com/start/login) if not logged in, in order to view the page.

Without using any plugin, what would be the condition logic which if user visits /start page, while they are not not logged in, then the user is redirected to /start/login page instead?

Thanks.

1

In a template file, this should do it:

<php
If (!is_user_logged_in())
{
   wp_redirect( "start/login" );
   exit();
}
?>
| improve this answer | |
0

If the login page you want to send visitors to is the regular WordPress login page, then you can use the built in auth_redirect function. Put this code at the top of your template file:

<?php auth_redirect(); ?>

This code will do the following:

If user is logged out

  1. Redirect the user to the default login page of WordPress.
  2. After login, redirects the user back to the current page.

If user is logged in

  • Page renders as normal, no login redirect.

More documentation: https://codex.wordpress.org/Function_Reference/auth_redirect

| improve this answer | |

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.