Redirect Users To Front Page After Login
A question that pops up in the forums from time to time and appeared again just the other day is how do I redirect users to the front page after they log in? In other words the admin didn't want his users being sent to the backend which is the WordPress default behaviour.
I'm sure there are several plugins in the official WordPress Plugin Directory that will accomplish the task although I haven't looked through them to find one. The solution I gave to the original poster, which I'll share with you here, is one that does not involve the use of a plugin.
Add the following to your theme's functions.php file...
/* redirect users to front page after login */
function redirect_to_front_page() {
global $redirect_to;
if (!isset($_GET['redirect_to'])) {
$redirect_to = get_option('siteurl');
}
}
add_action('login_form', 'redirect_to_front_page');


Thank you, but I have tried with your code and it doesn't work. At least with wp 2.8.6
Hi Mau,
I just tested it on a 2.8.6 install and it worked fine. What theme are you using?
Thanks for this post! I have a question though. How can I disable any non-admin user to access the wordpress site and should be able to access it only through the login page? Moreover, is it possible that the admin user would get to see the administration panel and not the site, upon logging in?
Your code above works for redirecting from the login page but the wordpress site is still accessible via localhost/wordpress URL even without logging in. Thanks in advance!
Hi snowivy,
I would take a look at this plugin. It allows you to setup redirects based on specific users, specific roles and specific levels.
Thanks a lot Len!
You're quite welcome.
I tried this today and I get an error:
Fatal error: Call to undefined function add_action() in....
I use the P2 theme.
Any suggestions?
Thanks
Actually after I reread the instructions I put the code in the THEME fuctions.php file and it this time it just gives me a blank screen??
Hi Voogah,
It may be something specific to that theme. I'll download and install it in a local environment and have a look.
Hi Voogah,
I just tested it using WordPress 2.9.2 and the P2 theme and it worked just fine. I inserted the code in the theme's functions.php file right after the bit that registers the sidebar.
To make it easier, you can download a copy of the altered file at the WordPress Pastebin.