Arrow

Redirect users to the WordPress front page after logging in

First published on May 19, 2008

By default, whenever you log in to your WordPress blog via wp-login.php, it loads the admin panel. However, if you’re using the Prologue theme or a similar theme that places the “write post” form on the front-end of your site, you probably want to present registered users with the front page instead:

Quickposts - Tumblr posting embedded into your front page

(The screenshot above is a tweaked version of QuickPost, a nice Tumblr-like plugin that enables you to make posts from outside of your admin panel.)

The best thing to do is to simply place a login form directly on the front page of your site.

However, some people simply like to access wp-login.php directly, so you need to add one more piece of functionality. Using the login_form() hook, you can create a plugin to load the front page of a site after a user logs in.

<?php
/*
Plugin Name: Redirect user to front page
Plugin URI: http://www.theblog.ca/wplogin-front-page
Description: When a user logs in via wp-login directly, redirect them to the front page.
Author: Peter
Version: 1.0
Author URI: http://www.theblog.ca
*/

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

Note that you can change the value of $redirect_to in order to redirect a user to a different page (such as their profile).

Download the redirect users to the front page via wp-login.php plugin

August 16, 2008 update: here’s a more complex solution where you can redirect users by username, role, and capability

September 8, 2008 update: I’ve built a more fully featured version of the plugin that works in WordPress 2.6.2 and up and enables you to manage the redirect rules in the WordPress admin panel

Arrow

21 Responses to “Redirect users to the WordPress front page after logging in”


  1. RobW says:

    Thanks, Peter. Works great for me and my private Prologue-themed blog.


  2. Evan says:

    Hey how do I redirect to a page that is not the home page? I want to redirect to a custom page that has a few links on it for the user to easily navigate a few dimensions of the site. Please let me know how to redirect to a custom page not the site home page.


  3. Peter says:

    Hi Evan, instead of

    $redirect_to = get_option(‘siteurl’);

    Use:

    $redirect_to = ‘http://yoursite/yourpageofchoice’;


  4. Steve says:

    How do I get the variable for the login name they used? I’d like to do if an IF statement so if the username equals one thing, it will take them to the dashboard, if it equals something else, it will take them to do the homepage.


  5. Peter says:

    Hi Steve, you can do something quite similar with the wp_login hook, which occurs later in the process and gives you the username (or alternatively, the a global $user object where you can also filter based on the user ID).


  6. Brian says:

    Hi, great script. Solved my problem. Thanks a bunch. I updated it though so that administrators still get redirected to the dashboard page. Maybe it’s a "fix" you want to incorporate into the original:

    <?php
    /*
    Plugin Name: Redirect user to front page
    Plugin URI: http://www.theblog.ca/wplogin-front-page
    Description: When a user logs in via wp-login.php directly, redirect them to the front page.
    Author: Peter
    Version: 1.0
    Author URI: http://www.theblog.ca
    */

    function redirect_to_front_page() {
    global $redirect_to;
    if (!(current_user_can(‘level_8′))) {
    if (!isset($_GET['redirect_to'])) {
    $redirect_to = get_option(‘siteurl’);
    }
    }
    }

    add_action(‘login_form’, ‘redirect_to_front_page’);
    ?>


  7. Brian says:

    … an even better idea would be to make some options for this plugin and one could be the page to redirect to and the other could be the user level that triggers redirection… just a thought, wish I had time to do it myself…


  8. Peter says:

    Hi Brian, your solution won’t work because WordPress does not know the current user’s capabilities when it’s at the "login_form" step.

    Here’s a working but much more complex solution which would be configured in the plugin file itself.


  9. Brian says:

    Yeah you’re right Peter, I thought I had tried it and it worked, but it seems that what I tried was to go to the siteurl/wp-admin and if I was logged out it would redirect me to the login page, but then it would keep me at the dashboard after I had logged in which is the effect I wanted. Maybe the same would have occurred using your script, I hadn’t tried it… anyway, it’s not really worth the trouble for me, I thought it was an easy solution… thanks either way. -Brian

    p.s. What plugin are you using to get the anti-spam feature of your submit comments?


  10. Peter says:

    Hi Brian, see http://www.theblog.ca/anti-spam for the plugin :D


  11. Nate says:

    Peter, I just wanted to let you know that this is an excellent plugin. I very luckily stumbled upon it. I used the "working but much more complex solution" and it did exactly what I needed to get done.

    One suggestion if you decide to continue to improve the plugin would be to allow users to configure the redirects by role, since you already have level and individual users.

    Regardless it’s a great plugin and thanks for the work you put into it.


  12. Peter says:

    Hi Nate, I’ve now updated the more complex version to include redirect per role.


  13. Kevin says:

    Hi Peter,
    I’m trying to get this to work, but I’m a little lost as to how to go about configuring the code. For example, I want to redirect via Subscriber role, so how do I add that?

    $redirect_by_role = array(); // Do not touch this line. Enter rules below this line

    When I enter:

    $redirect_by_role['subscriber'] = ‘http://www.example.com’;

    underneath the above line, nothing happens. I should be able to figure this out, but I’m not as familiar with PHP as I would like to be.


  14. Peter says:

    Yup, that’s how it works. Make sure you are redirecting to a page within your blog’s directory — without hacking the core files, WordPress does not allow you to redirect to external pages upon login.


  15. Kevin says:

    I missed the ";" at the end of my line… a simple error. However, now that it’s working, I’m not getting a redirect when I log in as a subscriber… And if I change the setting by level (level_0 = subscriber), I’m not getting a redirect either.


  16. Peter says:

    Sorry Kevin, I don’t have any other ideas except maybe the plugin wasn’t activated again after you fixed your syntax error? Otherwise, if you want to send me some temporary login details, I can check it out.


  17. bob says:

    simple way.. http://nathany.com/developer/redirecting-wordpress-subscribers/

    worked for me :)


  18. Mario Zuany says:

    Man, I just want to thank you for this! =)


  19. Ted says:

    Many thanks – excellent plugin


  20. Tony says:

    I use your plug in to lock down my blogs so that people have to log in to view the entire blog.

    Right now I redirect my subscribers to my homepage but they complain that they want to stay on that blog page that they logged in on. How do I program your plug in so the user stays on whatever page they were on when they logged in?

    I don’t want to send them to an arbitrary page, I want them to see the full that they were on, whatever that page may be, before they logged in.

    Reply from Peter: Please see this comment


  21. Dave Carter says:

    Peter

    It worked like a charm!
    Thanks a lot for this useful piece of code :D

Speak your mind

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word