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?
- Redirect after registration and auto login is not working
- Redirect user to original url after login?
- wp_redirect() function is not working
- Redirect page URL to home URL without using a plugin
- Login redirect to previous page
- Why is wp_redirect() preferable to a standard PHP header redirect?
- Redirect to referring page after logging in
- check first time login only
- How to change the default logout link on WordPress Admin
- How to redirect a sucessful registration to a page template?
- 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
- Redirect to current page after wordpress login
- Redirect to “All Posts” after post update or publish in Block Editor
- Redirecting old site links to new site
- Temporary redirect prevents getting $_POST array
- Creating intro page in wordpress
- URL Redirect and Bulk Actions in wp_list_table
- How to redirect only if page doesn’t exists
- Headers already sent – WordPress core
- How to redirect action=register link on the lostpassword page to a different link?
- You do not have sufficient permissions to access this page
- Redirect user after logout
- how to do logout redirect to current url
- Perform a redirect after user action
- How To Only Allow Users To View Their Own Buddypress Profiles? [closed]
- 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
- header and wp_redirect not working. cannot modify header information warning
- Code to create a redirection after login?
- How do I redirect /search/ to ?s=
- Changing “Lost Password Email Link” to custom password reset page
- WordPress Global Redirect
- Why does wp_redirect strip out %0A (url encoded new line character) and how do I make it stop?
- How to redirect user to a specific page based on username?
- Redirect if $_Server[‘REQUEST_URI’] is /wp-login.php?checkemail=registered
- Redirect after password update
- Redirect the non-www version of the site to the www
- How to redirect user after login to a specific page?
- How to use Regex to add to a url?
- Redirect first category archive page to normal page
- Stuck In a Redirect Loop
- Redirect when accessing /login when logged in
- How to redirect after login getting a variable from url (for example with the language)
- How to set 301 redirection after moving WordPress blog?
- If User is NOT Logged in and Page ID is not 6 – Redirect to Login
- Redirect to Current Page after Login
- How to include error message on login
- Login redirect problem
- Can I change default registration link (without htaccess)?
- Front end user creation form! How do you keep the user on the same page in event of error?
- How to redirect on login to a specific page if a specific meta user is empty
- Adding rewrite rule dynamically for search results redirecting to 404 URL
- Force WordPress to load from site’s hostname
- How to redirect if a background request is still being executed
- redirect Login problems
- Redirect custom post type from one domain to another domain
- How To Redirect /url.html to /url?
- I get redirected too many times only in wp-admin?
- How can I force the user to log in, even if they’re already authenticated?
- User is logged in after Stripe payment, then redirected, but is then logged out again
- Get query parameters from external url and make a redirection
- Redirect users after register (first login) and force submit post
- Login Redirect – Multiple Scenarios
- wordpress_redirect returns different headers for HEAD and GET requests
- Remove #wpcf7-f2450-o1 with Contact form 7 redirect [closed]
- WooCommerce – Redirect to a product after login
- How to pass a message using template_redirect
- Usage of the login_redirect filter
- Show different page for first time user
- Redirect user to original url after login?
- Redirect outside WP after login
- Prevent users from going to wordpress profile after login
- HTML Redirect to WP pages
- Why does the $_GET parameter ?search forward the front-page to the archive/blog page