If I understood correctly, you need to check if the user is logged in, if not, redirect it to login page. If the user logs in succesfully, he should be redirected to the page he was trying to see. You can do this by using the redirect
argument of wp_login_url()
.
This code should work (not tested):
add_action( 'init', 'cyb_restrict_guest_access' );
function cyb_restrict_guest_access() {
global $wp;
if( ! is_user_logged_in() && ! cyb_is_login_page() ) {
wp_redirect( wp_login_url( site_url( $wp->request ) ) );
exit;
}
}
function cyb_is_login_page() {
return in_array($GLOBALS['pagenow'], array('wp-login.php'));
}
Related Posts:
- Password change when the user login first time
- wp-login.php — redirect logged in users to custom URL
- Redirect user after login/registration globally
- Redirect Logged In User if page is wp-login.php and $_Get[‘level’] = X
- Redirect user to previous page after signup from custom form
- Redirect User to login page
- Trying to re-direct users to specific page based on an ACF variable
- Keep the old referrer after failed login
- wp_redirect() function is not working
- Redirect page URL to home URL without using a plugin
- Login redirect to previous page
- check first time login only
- How to change the default logout link on WordPress Admin
- WordPress HTTPS redirect loop
- How to redirect after login, the working way?
- Custom login form redirect to external site
- handling login/logout redirects
- Restrict Access to wp-login.php
- Using `auth_redirect` : keeps asking me to login even when I’m logged in
- Redirect users after first login
- Stop unwanted WP redirection to new url
- Redirect to current page after wordpress login
- 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
- How do I redirect a user when he is logged in after clicking on a menu
- 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=
- How to redirect to home page after registration?
- Redirect after password update
- How to redirect user after login to a specific page?
- How to use Regex to add to a url?
- 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
- Adding rewrite rule dynamically for search results redirecting to 404 URL
- How to redirect if a background request is still being executed
- 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
- Login Redirect – Multiple Scenarios
- wordpress_redirect returns different headers for HEAD and GET requests
- Usage of the login_redirect filter
- Show different page for first time user
- Login/Register redirect user with message
- Redirect user to original url after login?
- Redirect outside WP after login
- 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 do I write a subdomain redirect?
- How to fix automatic redirects?
- 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?
- Add base folder to CPT and 301
- 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/
- How to automatically redirect category pages (301) when changing the location of a category in the hierarchy
- 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
- How to disable login and registration pages?
- Post Migration Site Migration Redirects All Known Solutions Attempted
- wp_redirect() doesn’t work
- Redirecting logged in users to custom URL