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

post
About Len Kutchma

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. Be sure to follow @wpcanada on Twitter.

Comments

  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.

  5. Cindy says:

    Hi,

    I just tried the code above (redirect) and it works great--thank you so much! You seem to be very knowledgeable about the WP software and I'm wondering if you might know the answer to a rather bothersome issue: I have the P2 theme; For some reason, the text I type into my search box appears white--so you can't see it because the search box area is white, too. Would you happen to know where one would go to change that text color to black? I'm seeing so many options in the php files that I'm afraid to break something!

    Cindy

    • Len Kutchma says:

      Hi Cindy,

      I just installed that theme on a test site and can't duplicate that problem. I'm not sure why the text in the search form is the colour it is.

      One thing you can try. Changes such as this need to be made in the style sheet - a file named style.css. Look in that file for the following ...

      #searchform #s {
      width: 57%;
      }

      ... and add color: #000000;

      That block of code should now look like this ...

      #searchform #s {
      width: 57%;
      color:#000000;
      }

      The declaration #000000 will make the text in the search form appear black. You can adjust that to whatever you want.

  6. Cindy says:

    Hi,

    That did the trick! Thank you SO much!

    Cindy

Please Note: WPCanada is a moderated community. Please read the Comment Policy.

What Do You Think?

*