Here is from my previous project. The function is hooked into the template_redirect
action. Inside the function there are 2 conditionals. The first is the the one that will redirect logged in user away from the login page. And the other one is to redirect non logged in user to the login page.
// add a redirect for logged out user
add_action('template_redirect', 'redirect_user');
function redirect_user(){
global $current_user;
if (is_page('login-page-for-non-logged-in-user') && is_user_logged_in()){
$return_url = get_bloginfo('url');
wp_redirect($return_url);
}
// this part is untested
if (!is_page('login-page-for-non-logged-in-user') && !is_user_logged_in()){
$return_url = get_bloginfo('url');
wp_redirect($return_url);
}
// end of untested part
}
Related Posts:
- Redirect user after login/registration globally
- Password change when the user login first time
- Restrict Access to wp-login.php
- wp-login.php — redirect logged in users to custom URL
- Redirect to login page
- How do I redirect a user when he is logged in after clicking on a menu
- How to auto login after registration? [duplicate]
- How to modify the Register link in the login page?
- How to redirect to home page after registration?
- Redirect Logged In User if page is wp-login.php and $_Get[‘level’] = X
- Redirect user to previous page after signup from custom form
- Login/Register redirect user with message
- Why I am not able to direct user to my blog section without login?
- Trying to re-direct users to specific page based on an ACF variable
- Keep the old referrer after failed login
- How to redirect new registrars to a custom registration page instead of WP default registration page?
- How to disable login and registration pages?
- wp_redirect() function is not working
- Login redirect to previous page
- check first time login only
- WordPress HTTPS redirect loop
- How to redirect after login, the working way?
- Custom login form redirect to external site
- handling login/logout redirects
- Redirect to custom url when registration fails?
- Using `auth_redirect` : keeps asking me to login even when I’m logged in
- Redirect to custom URL after registering from a page with registration form
- Redirect users after first login
- Stop unwanted WP redirection to new url
- Temporary redirect prevents getting $_POST array
- URL Redirect and Bulk Actions in wp_list_table
- Redirect user after logout
- How to redirect user to specific page based on the input password
- Custom Registration page – prevent redirect to wp-login.php?action=register
- Firing a function AFTER redirect
- How do I redirect /search/ to ?s=
- How to redirect user to a specific page based on username?
- Redirect if $_Server[‘REQUEST_URI’] is /wp-login.php?checkemail=registered
- Stuck In a Redirect Loop
- Redirect when accessing /login when logged in
- If User is NOT Logged in and Page ID is not 6 – Redirect to Login
- Redirect to Current Page after Login
- Login redirect problem
- Front end user creation form! How do you keep the user on the same page in event of error?
- Adding rewrite rule dynamically for search results redirecting to 404 URL
- How to redirect if a background request is still being executed
- I get redirected too many times only in wp-admin?
- User is logged in after Stripe payment, then redirected, but is then logged out again
- Login Redirect – Multiple Scenarios
- Redirect user to original url after login?
- Redirect outside WP after login
- HTML Redirect to WP pages
- store URL global before we redirect
- Custom Login Form – Redirect user to login page if not logged in
- How to redirect from one WP site to another
- How to fix automatic redirects?
- Load function with login_redirect
- How to redirect user after login to the page called by his first name?
- redirect doesn’t work while using example code
- Redirect After Login Error
- Redirecting simple big problem
- Login redirect to previous page
- How to redirect a child page to its parent page?
- WordPress multi domain: page redirect to main domain whenever WordPress do the URL guessing issue
- Applying ‘middleware’ to a url before redirecting? Does WP have built in mechanism for this?
- Redirect to custom login page if not logged in and redirect to home if logged in user try to login, how?
- How do redirect user back to previous page when on certain pages?
- wp_login_form() redirect same page
- How do I redirect the browser to 404 page, if no posts are found in home page
- Hide a Post everywhere except on redirect
- Conditional Login Redirect
- How to redirect if 404 error in wordpress then redirect 404.php url
- ERR_TOO_MANY_REDIRECTS when switching from good database to old database
- Redirect to /404/
- Redirect user after login to prior page through a wp_login hook
- How can force redirected to login for two or more pages to view or access
- How to redirect user to Referrer Page using wp_signon in Custom Login Page in WordPress
- HOW do you Redirect buddypress login to EDIT tab not PROFILE tab on profile page? [closed]
- Login redirects to home page and doesn’t log in
- redirect after account activation for forum using bbpress and buddypress
- using wp_rewrite but keep the original url in the address bar
- redirect_to ignoring the SSL
- Redirect user on login using extra field value on login form
- Woocommerce login to specific page or referrer
- wp-login.php redirect problem
- login redirect problem – db charset issue?
- How to redirect 404s that generated for css&JS files?
- Redirect problem: How to redirect to the original URL after login
- Login Form Redirection
- login page not working [closed]
- Redirect after user changes password
- WordPress redirect to splash page once a day for the first week
- Accept (once only) policy page on login
- wp_login_form: Redirect to dynamic url according to username
- problem redirecting users to page on login
- Create a 301 Redirect automatically when adding %category% in the permalink
- Other pages fine but wp-admin showing ERR_TOO_MANY_REDIRECTS Error
- Post Migration Site Migration Redirects All Known Solutions Attempted
- wp_redirect() doesn’t work
- Redirecting logged in users to custom URL