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');

About the author
Len has been blogging for over 10 years and is a rabid WordPress fan. In addition to blogging here you can find him writing the occasional article and toiling away in the forums at WeblogToolsCollection.com. He also hangs out at the WordPress support forums lending a hand where he can. You can find his political scribblings at RiteTurnOnly.com

10 Responses to “Redirect Users To Front Page After Login”

  1. Mau says:

    Thank you, but I have tried with your code and it doesn't work. At least with wp 2.8.6

  2. snowivy says:

    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!

  3. Voogah says:

    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

  4. Len Kutchma says:

    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.

Leave a Reply

CommentLuv Enabled