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
- PHP header(Location: …): Force URL change in address bar
- What does HTTP/1.1 302 mean exactly?
- php refresh current page?
- Automatic WordPress Login of Logged In ClickFunnels User
- How to force a 404 on WordPress
- Disable front end to use as CMS only?
- Redirect Restricted Page to 404
- How to redirect non-logged in users to a specific page?
- How to prevent automatic redirection of 404 errors and “incorrect” URLs?
- How do I skip wordpress’s 404 handling and redirect all 404 errors for static files to 404.html?
- Disable WordPress URL auto complete
- Host the wp-admin on another domain?
- Redirect user to original url after login?
- When I change slug, will WordPress create redirect from the old URL?
- WordPress localhost site redirect to live site
- WordPress redirecting connections on port 8080 to 80
- WordPress Redirect All HTTP requests to HTTPS via .htaccess
- wp_redirect() function is not working
- Redirect entire website to a single page
- My wordpress multisite homepage redirects to signup page
- Disable ONLY URL auto complete, not the whole canonical URL system
- How to validate a user from ouside wordpress/php?
- Redirect Localhost wordpress site to dashboard
- Redirect page URL to home URL without using a plugin
- Why is WordPress redirecting from http to https on a local environment?
- Login redirect to previous page
- How to prevent redirect to ‘About’ after core upgrade?
- 301 redirect instead of 404 when URL is a prefix of a post or page name?
- Why is wp_redirect() preferable to a standard PHP header redirect?
- How do I programmatically generate a 404?
- why does WordPress need two cookies for auth/login
- Redirect to referring page after logging in
- User redirect to destination URL after login
- Prevent access to single post types
- check first time login only
- Stop WordPress from “guessing” redirects for nonexistent URLs
- WordPress install enters an infinite loop
- How to redirect a sucessful registration to a page template?
- How to configure nginx to redirect requests to the uploads directory to the production server?
- WordPress HTTPS redirect loop
- Why am I getting a 301 on a existing post?
- Redirect to dashboard after login
- Make a video post redirect to next post after completion
- Old blog/ and blog/feed/ URLs not working after moving blog to top-level via .htaccess
- Non-WordPress page in subdirectory under WordPress page
- Force HTTPS using .htaccess – stuck in redirect loop
- How to redirect after login, the working way?
- Masking logout URL
- Keeps on redirecting on my development copy
- Password change when the user login first time
- Custom login form redirect to external site
- Redirecting WordPress /.htaccess / HSTS / SSL
- Should wordpress_logged_in cookie exist while logged out?
- Keep Users Logged In As Long As I Like
- 301 redirect not working through .htaccess
- Clarification on auth_redirect()
- Clearing cookie on logout and session expiration
- Logout/login redirect CSS issue
- Site redirects to wrong url when saving settings
- How to redirect all requests to wp-login.php?
- Implementing global redirection (automatic redirect for all pages who had their slug changed)
- Correct way to hide pseudo pages from being shown?
- Redirect blog page to latest post?
- Redirect to custom url when registration fails?
- Why does WordPress automatically redirect URLs with the parameter “name=” to a different page?
- prevent/block direct access to a thank you page
- Authentication / login mechanism (non wp-admin)
- Redirect After Delete User in Backend
- Exclude a path from WordPress using .htaccess redirects (Apache)
- Restrict Access to wp-login.php