You should use the built in WordPress function is_user_logged_in()
, as well as several other WordPress functions:
if ( !is_user_logged_in() ) {
wp_redirect( get_bloginfo( 'url' ) . '/index.php' );
exit;
}
wp_redirect()
handles the redirection for you. Please be aware that it does not exit automatically, so you should call it afterwards.
I also built in the WordPress Function for getting your URL – this may not be necessary in your case, as you just redirect to the front page.
Be sure that this function is called before any output is sent.
Related Posts:
- Logout redirect to home page
- How to change the default logout link on WordPress Admin
- handling login/logout redirects
- Using `auth_redirect` : keeps asking me to login even when I’m logged in
- Setting a redirect cookie in wordpress
- WordPress Redirect based on the prescence of a cookie
- wp_logout_url() redirects to confirmation page even with nonce?
- Creating intro page in wordpress
- $_SESSION variables lost during OAuth callback
- Redirect user after logout
- homepage redirects to login page when user session expires
- Redirect after password update
- How can I force the user to log in, even if they’re already authenticated?
- Logout without confirmation and SAME window on mobile
- store URL global before we redirect
- Custom Login Form – Redirect user to login page if not logged in
- How can I set up a splash page that is redirected to only one time for the visitor? [closed]
- redirect “about author” code to about page
- How to redirect logged out users to specific page?
- Login redirect shows white page in firefox
- Change auth_cookie_expiration for specific roles and specific login times
- Changing auth_redirect() page
- How to redirect returning users who previously logged in?
- Conditional redirection with logout
- Custom, conditional redirect on logout
- Logging back into WordPress after logging out and having issues with ‘loggedout=true’ in redirect URL
- after logout, get_current_user_id still holds value
- Accept (once only) policy page on login
- WP Logs me out after updating anything on dashboard
- WordPress Logout Redirect Follow up
- How to redirect non-logged in users to a specific page?
- Why is WordPress redirecting from http to https on a local environment?
- Non-WordPress page in subdirectory under WordPress page
- Implementing global redirection (automatic redirect for all pages who had their slug changed)
- Redirect to custom url when registration fails?
- Restrict Access to wp-login.php
- How to redirect url requests to https? [closed]
- Best way to handle redirects
- WordPress within a subdirectory of another WordPress environment doesn’t work
- wp-login.php — redirect logged in users to custom URL
- How to do 301 redirect to Sub page using htaccess file?
- Redirect feed to 404 page
- Does WordPress automatically redirect url without parent slug?
- Hiding WordPress behind IIS redirects
- Localhost wordpress site redirects to public site
- Redirect homepage ( www.sitename.com ) to the latest post
- Redirect all post into some category to url without category
- Redirect from sub-directory to root – New WordPress 5.2.3 Set up
- What is the right way to redirect all traffic to HTTPS?
- Where’s the redirect happening? [closed]
- Front end user creation form! How do you keep the user on the same page in event of error?
- URL redirect problem
- WP site in infinite redirect loop
- Regex with HttAccess
- Regex redirect rule for missing part of the path
- Page template that redirects user based on role
- How can i resolve 301 redirection Issue on my website?
- https – too many redirects
- Redirect outside WP after login
- Redirect non existing page to frontpage
- How do I do a redirect to WordPress permalink with post id via htaccess?
- How to prevent redirect 301
- How to redirect multiple subfolders back to root domain?
- Redirect After Login Error
- Is it possible to detect if a POST action is for a “Save Post” before the save_post action?
- Disable and Hide WP-JSON
- URL redirection
- Old missing file in wp-content/uploads/ returns a 500 error, how to catch and redirect?
- Browsers Forcing Multisite to https
- Is it possible to change redirect template through a shortcode?
- How do redirect user back to previous page when on certain pages?
- How to 301 redirect WP posts id “/c/94940894” to permalink with post name?
- WP 301 redirect does not work
- Redirect Image URL to it’s parent post
- WordPress Redirect post to outside website based on useragent
- WordPress site not forcing from http to https instead getting redirected too many times
- Redirect not work
- There is 1 redirect
- Redirect to custom admin menu after plugin activation
- All new posts serving 301 redirect
- home page redirect loop after update to 4.8
- Moving site from HTTP to HTTPS
- Redirect from plugin created page
- How is this URL being redirected?
- What’s the best way to redirect to a new wordpress website?
- 301 Redirect only for posts
- Custom redirects with using SQL
- Redirect after user changes password
- Getting error when trying to redirect WP site to mobile site
- wp_login_form: Redirect to dynamic url according to username
- Hide page for user logged and redirect for profile? [duplicate]
- Redirect URL while building site
- Redirect ignore last URL segment
- Creating custom link that redirects to a another link
- Other pages fine but wp-admin showing ERR_TOO_MANY_REDIRECTS Error
- How to disable login and registration pages?
- Forcing header cache control in 404 pages
- Regex for pdf’s – Old .asp Site folder structure to new WordPress Media Folder(s)
- Create a custom redirect link that tracks the first visited page and after submitting form goes to first page using formidable forms plugin? [closed]
- WordPress not use site url when redirect 301