# WordPress.org

## change login redirect (27 posts)

1. dmarkd
Member
Posted 10 years ago #

I've setup WP1.5 and am in the process of setting up my own templates for it. However, I'm trying to figure out how to make it so that after a user logs in, they aren't automatically redirected to the profile page, but instead are taken to the main page (in reality the site should take them to the page they were on when they clicked login). After looking through the forums here a bit and googling, I've found a few different solutions but nothing seems to be working. I've also been digging through the wp-login.php file itself and whenever I make changes I seem to end up breaking the login rather than changing the redirect. Is there a simpler way to do this? If so, why isn't it an option in the administrative control panel?

Thanks,

Mark

2. dmarkd
Member
Posted 10 years ago #

OK so I've noticed that if I change $redirect_to in wp-login.php I can SORT OF control where users are sent upon login. However, it only seems like my admin login is using that value, whereas users I have added after setting up the site are being redirected to the profile page. Any ideas what is happening? 3. dmarkd Member Posted 10 years ago # More self discovery - obviously making the changes to$redirect_to only effects the LOGIN redirection, and does nothing to the logout. So I've had to change the header redirect value under the case:logout section as well. Unfortunately, I'm still having issues with regular users being appropriately redirected, only the admin account is being redirected appropriately.

4. ColdForged
Member
Posted 10 years ago #

why isn't it an option in the administrative control panel

Because if every esoteric option was in the administrative control panel we'd need a 3-monitor system and a gig of server space to host the admin pages :).

That said, which of the many $redirect_to assignments are you modifying? 5. dmarkd Member Posted 10 years ago # Because if every esoteric option was in the administrative control panel we'd need a 3-monitor system and a gig of server space to host the admin pages :). I'm not so sure I agree that this is an esoteric option. I definately agree that every option can't be catered to, but at the very least an option between "redirect to last page" or "redirect to profile page" should exist. The majority of times someone logs into a site they are not going to be modifying their profile settings, but rather accessing content they have access to as a registered user. Anyway, that aside, I'm modifying the first$redirect_to assignment under the default: case - the rest I've left alone.

6. ColdForged
Member
Posted 10 years ago #

I'm not so sure I agree that this is an esoteric option.

Granted you might think it's not, but for the seeming majority of WordPress users -- single-author blogs -- an option to control where the login redirect goes isn't overly necessary.

Try modifying this one on line 178.

$redirect_to = get_settings('siteurl') . '/wp-admin/profile.php'; That's likely the one getting hit in those cases. 7. vkaryl Member Posted 10 years ago # dmarkd, if the line CF gave you works, could you post the relevant code-tweak? I'd like this better myself, but I am NO php code-jockey! Talk about breaking things.... 8. jweaks Member Posted 10 years ago # I think this is a great idea. Ideally it would direct you back to where you were. The line CF identified is helpful. At first, it appeared to have no effect and then I realized the code has a user = level 0 conditional. So I changed that redirect by putting a semi-colon after ('siteurl') and then a space and then slash-slash to comment out the rest of the line. That way I can undo it easy. That redirects level zeros to the home page. Then I copied the two lines, the "if" line and the "$redirect" line and then pasted them back in below the original. I then changed that user level from 0 to 1.

Default users are, I believe a level 1, so zeros and ones now get redirected to the home page.

That's probably not the correct PHP way to do it, but it works.

9. vkaryl
Member
Posted 10 years ago #

Hey, thanks, jweaks. I'll give that a shot.

10. whooami
Member
Posted 10 years ago #

Im pretty sure default users are a level 0, same as anonymous or unregistered users. Once you are a 1 you can actually do things beyond commenting, check the codex on user levels.

I went through figuring all of that out setting up my login popup.

11. jweaks
Member
Posted 10 years ago #

On my site the default user level is 1. I think I caused it to be that way by changing the writing option to "May submit drafts for review."

I think that bumps new registrants up to 1 so they can submit. I'll bet level 0 is the default at installation.

If you change write permissions to "May publish..." I'll bet the new user level goes up again. -jw

12. csaft
Member
Posted 10 years ago #

Ok, I am trying to follow the suggestions here, but where exactly am I looking for line 178: $redirect_to = get_settings('siteurl') . '/wp-admin/profile.php'; Is it in the main template, or somewhere else? 13. moshu Member Posted 10 years ago # 14. tbrams Member Posted 9 years ago # I have done this for my local Taekwondo Club website. It's really not a lot you will have to do and I have tried to be very specific documenting. See the step by step instruction at : Hope this is useful. 15. hemp Member Posted 9 years ago # This behaviour applies to almost all users except level 0s. To have it also for them just comment out the following line in wp-login.php: if ( 0 ==$user->user_level )
$redirect_to = get_settings('siteurl') . '/wp-admin/profile.php'; 16. lexiron Member Posted 9 years ago # I also wanted this functionality, but if you just hack the wp-login.php, users COULD still go to wp-admin (from the link on the site or by typing it in) so I wrote a short thing to plugin to auth_redirect() - so before it goes to any wp-admin page, so leave wp-login.php going there, and then this will redirect. I haven't written any plugins before or know php really at all, but it works for me so Ill post it here. I had it so under user_level 2 goes directly to the site, and am using it on wordpress 2.0. So they never can edit/change their profile, this is just so I can give 10 friends the same user/pwd to read protected posts, I don't need them able to change anything or see the dashboard. <?php if ( !function_exists('auth_redirect') ) : function auth_redirect() { // Checks if a user is logged in, if not redirects them to the login page if ( (!empty($_COOKIE[USER_COOKIE]) &&
!wp_login($_COOKIE[USER_COOKIE],$_COOKIE[PASS_COOKIE], true)) ||
(empty($_COOKIE[USER_COOKIE])) ) { nocache_headers(); header('Location: ' . get_settings('siteurl') . '/wp-login.php?redirect_to=' . urlencode($_SERVER['REQUEST_URI']));
exit();
}
else {
if (!empty($_COOKIE[USER_COOKIE] )) {$user_login = $COOKIE[USER_COOKIE];$userdata = get_userdatabylogin($user_login); // If the user can't edit posts, send them to site if ( !$userdata->user_level <= 2){
exit();
}

}
}
}
endif;
?>

17. lexiron
Member
Posted 9 years ago #

ah, for wordpress 2.0 this will only work if you've added user_levels (through a role manager)

if you just want to send subscribers and such straight in, use

if (!current_user_can('edit_posts')){

18. edasque
Member
Posted 9 years ago #

I am unclear as to what the procedure is on WP 2.0. I have the same problem with new users (subscriber level) being re-directed to the dashboard/user-profile page when I'd like them to be sent back to the main blog page.

19. jrbeaudry
Member
Posted 8 years ago #

20. Pizdin Dim
Member
Posted 8 years ago #

What would be nice and versatile is to have WP "remember" the users last page and redirect there when you login. The hacks would have to consist of:

1. When user clicks on "logout", record the url of the "current" page, preferably in the database.
2. When user logs in, get the url from the database and redirect there.

A new column in the "users" table would be needed to record the last url.

21. coldfusion055
Member
Posted 8 years ago #

Heres what i came up with...

In wp-login.php on line 172 (WP 2.0.4), change:

$redirect_to = 'wp-admin/'; to$redirect_to = 'YOUR BLOG INDEX';

line 196

$redirect_to == 'wp-admin/' to$redirect_to == 'YOUR BLOG INDEX'

and on line 197

$redirect_to = get_settings('siteurl') . '/wp-admin/profile.php'; to$redirect_to = get_settings('YOUR BLOG INDEX') . ' ';

hope that helps.

http://www.brokenmachines.net to see it in action.

22. Jinsan
Member
Posted 8 years ago #

could someone explain what exactly you replace YOUR BLOG INDEX with? An example if you will as I'm not sure what my blog index would be. Also can anyone else confirm this works?

23. coldfusion055, while trying to be helpful, doesn't know what they're doing, so I wouldn't listen to them if I were you.

Wherever $redirect_to is manually defined, just change that to something else, like this: if ( !isset($_REQUEST['redirect_to'] ) ) $redirect_to = 'http://yoursite.com/'; else$redirect_to = $_REQUEST['redirect_to']; 24. Xabbu Member Posted 8 years ago # This seems to work as well: I added a line in the file wp-login.php just beneath line 5:  line 5:$error = ''; line 6: if (!isset($_REQUEST['redirect_to']) )$_REQUEST['redirect_to'] = get_settings('siteurl'); \to redirect back to main site. 

This redirects everyone back to the main page afer login or logout.

Greets,

Xabbu

25. vkaryl
Member
Posted 8 years ago #

Keep in mind that this type of core hack has to be redone whenever wp is upgraded....

26. jpeavey
Member
Posted 8 years ago #

For what it's worth, here is a plugin I wrote a while back to do this, I took the idea from another plugin I was playing with but couldn't get to work (Weasel's Login Redirect /TextChange http://www.tehblitz.org/?page_id=433).

$cat login-stayput.php  <?php /* Plugin Name: Login Stayput Plugin URI: Description: Sets redirect_to on wp_loginout function to keep users on the same page. Inspired from Weasel's Login Redirect / Textchange Author: Justin S. Peavey Version: 0.5 Author URI: http://www.peavey.org */ function jsp_loginredirect($link) { // Add redirect_to argument $from =$_SERVER['REQUEST_URI']; if (substr_count($link,'?') > 0) {$link = str_replace('">','&redirect_to='.$from.'">',$link); } else { $link = str_replace('">','?redirect_to='.$from.'">',$link); } return$link; } add_filter('loginout','jsp_loginredirect'); ?> 

27. Strange
Member
Posted 8 years ago #

jpeavey, when I install and activate this in WP 2.0.6 I get a rambling error message about header info, ditto when it is executed on logging in, any ideas?

This topic has been closed to new replies.