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
- 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
- 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
- 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
- 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
- How do I redirect a user when he is logged in after clicking on a menu
- Firing a function AFTER redirect
- How to auto login after registration? [duplicate]
- How to modify the Register link in the login page?
- 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?
- How to redirect to home page after registration?
- 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
- 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
- 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
- Login/Register redirect user with message
- 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
- store URL global before we redirect
- Custom form redirect after login
- Custom redirect user after login based on metadata
- wp_redirect () doesn’t work in nginx?
- Is it possible to wp_redirect() to a new tab?
- Custom Login Form – Redirect user to login page if not logged in
- Redirection on Custom page
- Redirect Attachment Page to Attachment
- How to redirect from one WP site to another
- How do I write a subdomain redirect?
- How to fix automatic redirects?
- Redirect user to specific link after login
- Create header.php redirect in WordPress and with WPML