First of all, home_url
doesn’t provide anything. You should use the is_home()
function instead.
After that, you need to exit the script when you are trying to redirect. So, your full code should be like this:
if ( is_user_logged_in() && is_home() ) {
wp_redirect('http://example.com/') ;
exit();
}
You should also hook into the right action hook. The proper hook to use for redirection is the template_redirect
:
add_action ( 'template_redirect', 'redirect_my_homepage' );
function redirect_my_homepage(){
if ( is_user_logged_in() && is_home() ) {
wp_redirect('http://example.com/') ;
exit();
}
}
Related Posts:
- How to turn off redirection from ‘domain.com/login’ to ‘domain.com/wp-login.php’
- Problem with login form
- How to redirect users based on role and content of redirect_to?
- Infinite loop when logging out using custom login form
- Need help with AJAX login to call php in functions.php to handle redirects based on user cap (role)
- wp-login.php?redirect_to=https problem
- Redirect users not logged in to the standard login page (and back) from some posts and pages
- wp_login_url always redirects me to wp-admin
- How do I make a redirect in PHP?
- User registration followed by automatic login
- Adding “Remember Me” in custom login
- How to change the wp-login.php page title?
- How build a custom login/register form with error handling?
- PHP – redirect https to http and www to non-www
- Change the footer text on the login page
- Changing user_nicename
- Redirect to Page after Post Submit
- How to redirect users to custom lostpassword page?
- WordPress 4 invalid username special charachters issue
- How to place login logout link on menu that redirects users back to current page?
- Insert code when users come from an specific referer
- Is Auto Post recognition a WordPress feature? How to turn it off?
- Using a nonce in a Custom Login Form
- WordPress template_include filter not working properly
- Constructing a custom login form using ajax
- automated tests as a user?
- Logout and redirect using the WP Menu
- Remove username in emails or swap username for email
- WordPress shows registration link for non logged users
- Admin username and password
- Recovering WP Login Credintials in Code?
- Is it necessary to sanitize wp_set_password user input?
- WordPress custom login form using Ajax
- WordPress does not load page.php, return 404.php
- How to give new users two specific user role options upon WordPress user registration
- How to: PHP Log Out Link?
- Login/logout in header
- Why is my cookie not unsetting upon logout? [closed]
- User management system similar to wordpress one?
- Programmatic Login from 3rd Party site
- getting logged in user info (wp-load.php) from parent directory
- How do I display the main domain for my WordPress install?
- Set Cache-Control header for 301 redirects
- Moving code from theme header to functions.php
- Showing latest post without 301 redirect
- Having Issue on Redirecting With Session in WordPress
- Using a $GET parameter from a URL, to redirect to a URL (WordPress)
- Using Argument from Function to Re-Direct Visitor (WordPress)
- Login redirect. Check user meta and redirect accordingly
- How to allow download url redirection only if user logged in WordPress site?
- Lost in trying to create user database system
- PHP Redirect condition
- PHP getting error when trying to access WP-Admin Dashboard
- If user is logged in not working
- Manually Create PHP Page For A Redirect
- Show login greeting above sub-menu links?
- Writing a link cloaking plugin
- WP insert post Redirect after function has executed
- Help with accessing wp-admin page and resolving error messages
- How to define a rule in functions.php that ONLY disables the default wp functionality that undesirably changes ‘&’ to ‘#038;’?
- Download file and redirect to page or vice versa
- Add more user roles to a PHP logout redirect function [closed]
- Removing “There is no account with that username or email address.” error message in “/wp-login.php?action=lostpassword”
- Can’t log in to WordPress wp-admin after adding code to functions.php
- 404 on old link after changing post url
- how can i redirect dynamic URL in worpress base on ID in htaccess or wp-config or function.php
- PHP warning – Use of undefined constant ‘FORCE_SSL_LOGIN’ ‘FORCE_SSL_ADMIN’ on wp-config.php
- Log out without confirmation request (nonce)
- Having trouble creating two shortcodes, one for logged in user and one for visitors
- Call WP Rest-Api to GET /users/me returned NOTHING in console
- Menu not updating for logged in users after redirect
- how to use auth_redirect() redirect visitor to login page if they are not login when they click account and order page?
- Issues adding Recaptcha v3 to WordPress Registration
- Display specific page if user signed in
- Change Login or Logout text based on status
- Refresh page after login with litespeed cache
- Redirect after login depending on the URL
- Redirecting the lost password page request when using a custon login page
- How do I send a POST request with params with WordPress REST API
- is_user_logged_in() not working in homepage
- ERR_TOO_MANY_REDIRECTS / To Force SSL Logins and SSL Admin Access
- WordPress Redirect / Add_Rewrite_Rule – Non Index.php Page
- Check if a user is logged into my WordPress site which is on a different server
- Newbie question. Login/Registration. New PHP page
- Submit to itself don’t work
- Adding a sidebar to wp-login.php
- redirect 301 with special character like WIX site “#!”
- 301 redirect from webpage to wordpress page in the same root
- List users in a dropdown for login
- White screen after login attempt
- 3 domains, 1 wordpress install, redirecting and changing domains on live site
- Get permalink for a post from inside WordPress and route to a related site
- How to replace wp-admin login page to another location?
- How to use multiple 404 Error Pages in WordPress
- Check user last login date
- login redirect based on user role not work as expected
- How to create a WordPress PAGE in another folder?
- Redirect loop upon installation of my plugin
- How do I do so that people can register on my wordpress site?
- change div text and link for logged in users