Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Change log in code for redirections WordPress

  • SOLVED

Hi all!

I have this code in the header.php:
<?php global $user_login;
if (is_user_logged_in()) {
echo '<p>Hello, ', $user_login, '. <a href="', wp_logout_url(), '" title="Logout">Logout</a></p>';
} else {?>
<span style="color:#FF7800;font-weight:bold;float:left;margin-top:5px;">Login</font></span>
<form action="<?php echo get_option('home'); ?>/wp-login.php" method="post">
<input type="text" name="log" id="log" value="User ID" onclick="this.value='';" onblur="this.value=!this.value?'User ID':this.value;" />
<input type="password" name="pwd" id="pwd" value="Password" onclick="this.value='';" onblur="this.value=!this.value?'Password':this.value;" />
<input type="submit" name="submit" value="Login" class="button" />
<input type="hidden" name="redirect_to" value="http://example.com/users-only/" />
</form>
<?php } ?>


Please advice how to rewrite the code for this/or how to solve this:

1. After Login the User/Admin should be redirected to the page http://example.com/users-only/

2. When Unregistered/Anonymous try to login the redirect page now is the standard Login Panel wp-login.php?loggedout=true, but it should be http://example.com/login-tips/

3. After Logout the User/Admin should be redirected to the Home page instead of the Login Panel wp-login.php?loggedout=true

Also the "Login Configurator" plugin is used but it's only for redirecting from standard Login Panel to the http://example.com/users-only/

Thanks.

Answers (3)

2011-04-27

Daniele Raimondi answers:

Maybe this could help:

[[LINK href="http://wpmu.org/login-and-logout-redirect-for-wordpress/"]]http://wpmu.org/login-and-logout-redirect-for-wordpress/[[/LINK]]

Here is another "redirect on login" plugin:

[[LINK href="http://wordpress.org/extend/plugins/peters-login-redirect/"]]
http://wordpress.org/extend/plugins/peters-login-redirect/ [[/LINK]]


Alex B. comments:

Daniele,

First plugin is a premium for WPMU. I have simple WP installation.

Second plugin creates only redirects after login but not 2 and 3.


Alex B. comments:

Sorry, first plugin is free after registration and can solve #3 but still the problem with #2.


Daniele Raimondi comments:

first plugin WAS only a WPMU plugin but the author has created a free WP monosite version:

"We’ve just made the WPMU DEV log out and log in redirect plugins work for regular old individual installations of WordPress too."

All you have to do is to register (for free) [[LINK href="http://premium.wpmudev.org/project/logout-redirect"]]here[[/LINK]] and then download it.

That should work for points 1) and 3). for point 2 I don't have a clue yet.


Daniele Raimondi comments:

Do you already have a login form on page http://example.com/login-tips/ ? If it's the case, all you need (i think) is a redirection (via htaccess for example) from standard login page to your custom login page.

2011-04-27

Julian Lannigan answers:

Alex,

Hopefully, this solves your list.

Add the following to your functions.php of your current theme.
function override_logout_form() {
wp_logout();
$logoutDestination = home_url('/');
$redirect_to = !empty($_REQUEST['redirect_to']) ? $_REQUEST['redirect_to'] : $logoutDestination;
wp_safe_redirect($redirect_to);
exit();
}
add_action('login_form_logout', 'override_logout_form');

function override_login_form() {
if ('POST' == $_SERVER['REQUEST_METHOD']) {return;}
$loginFormDestination = home_url('/login-tips');
wp_safe_redirect($loginFormDestination);
exit();
}
add_action('login_form_login', 'override_login_form');

function override_login_redirect($redirect, $request, $user) {
if ($request== '') {
return home_url('/users-only');
} else {
return $request;
}
}
add_filter('login_redirect', 'override_login_redirect', 1, 3);


For the last function "override_login_redirect" to work correctly you are probably going to want to deactivate any plugin you have installed that controls the redirect of the login. Now the above code is very basic in that it does not discriminate against user roles. If you want to do something like, have the admins redirect to the wp backend on login and everyone else redirect to "/users-only", change the "override_login_redirect" to the following:

function override_login_redirect($redirect, $request, $user) {
if (isset($user->roles) && in_array('administrator', $user->roles)) {
return admin_url();
} else if ($request== '') {
return home_url('/users-only');
} else {
return $request;
}
}


Let me know.


Alex B. comments:

Julian,

Great solution! But the #2 still doesn't work: when I enter wrong login data it redirects to wp-login.php


Julian Lannigan comments:

I think corrected the problem. The filter was not checking to see if there was an error with the login. Now it does and redirects accordingly.
function override_logout_form() {
wp_logout();
$logoutDestination = '/';
$redirect_to = !empty($_REQUEST['redirect_to']) ? $_REQUEST['redirect_to'] : $logoutDestination;
wp_safe_redirect($redirect_to);
exit();
}
add_action('login_form_logout', 'override_logout_form');

function override_login_form() {
if ('POST' == $_SERVER['REQUEST_METHOD']) {return;}
$loginFormDestination = '/login-tips/';
wp_safe_redirect($loginFormDestination);
exit();
}
add_action('login_form_login', 'override_login_form');

function override_login_redirect($redirect, $request, $user) {
if (is_wp_error($user)) {
//If you wanted to here, you could do something that will allow you to tell "login-tips" what the error was
wp_safe_redirect(home_url('/login-tips/'));
exit;
}

if (isset($user->roles) && in_array('administrator', $user->roles)) {
return admin_url();
} else if ($request== '') {
return home_url('/users-only');
} else {
return $request;
}
}
add_filter('login_redirect', 'override_login_redirect', 1, 3);


Here it is on Gist: [[LINK href="https://gist.github.com/947141"]]https://gist.github.com/947141[[/LINK]]


Julian Lannigan comments:

*I think I corrected.... ;)


Alex B. comments:

Works great, thanks!


Julian Lannigan comments:

Great. No problem, just remember to close the question and vote for me!

Thanks!

2011-04-27

David Navarrete answers:

try with this on top of header.php


<?php global $user_login, $wp_roles;


if (is_user_logged_in() && in_array('admin', $wp_roles)) {

wp_redirect('http://example.com/users-only/');

} else {
wp_redirect('http://example.com/login-tips/');

?>