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:
(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
July 21st, 2008 at 11:41 am
RobW says:
Thanks, Peter. Works great for me and my private Prologue-themed blog.
August 3rd, 2008 at 10:35 pm
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.
August 3rd, 2008 at 10:37 pm
Peter says:
Hi Evan, instead of
$redirect_to = get_option(‘siteurl’);
Use:
$redirect_to = ‘http://yoursite/yourpageofchoice';
August 4th, 2008 at 8:16 am
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.
August 4th, 2008 at 7:28 pm
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).
August 9th, 2008 at 7:50 am
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’);
?>
August 9th, 2008 at 7:51 am
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…
August 10th, 2008 at 2:16 pm
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.
August 15th, 2008 at 7:32 pm
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?
August 16th, 2008 at 12:32 am
Peter says:
Hi Brian, see http://www.theblog.ca/anti-spam for the plugin
August 16th, 2008 at 1:20 am
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.
August 16th, 2008 at 7:39 pm
Peter says:
Hi Nate, I’ve now updated the more complex version to include redirect per role.
August 19th, 2008 at 7:44 pm
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.
August 19th, 2008 at 8:59 pm
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.
August 20th, 2008 at 5:12 pm
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.
August 20th, 2008 at 10:04 pm
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.
September 22nd, 2008 at 1:53 pm
bob says:
simple way.. http://nathany.com/developer/redirecting-wordpress-subscribers/
worked for me
January 11th, 2009 at 7:50 pm
Mario Zuany says:
Man, I just want to thank you for this! =)
July 15th, 2010 at 10:58 pm
Ted says:
Many thanks – excellent plugin
November 11th, 2010 at 7:42 am
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
February 28th, 2012 at 4:12 am
Dave Carter says:
Peter
It worked like a charm!
Thanks a lot for this useful piece of code