admin管理员组文章数量:1202390
I'm using custom menu for login/logout here below is my code:
<li>
<?php
if (is_user_logged_in()) {
$user = wp_get_current_user();
echo 'Welcome <strong><a href=".php" >'.$user->user_firstname.'</a></strong>
| <a href=".php?action=logout"">Logout</a>';
} else { ?>
<strong><?php wp_loginout(); ?></strong>
or <a href="<?php bloginfo('url') ?>/wp-login.php?action=register"> <strong>Register</strong></a>
<?php }?>
</li>
but problem is :
redirect to login page not current page...
I want it should go to home page after logout. Please help me...
I'm using custom menu for login/logout here below is my code:
<li>
<?php
if (is_user_logged_in()) {
$user = wp_get_current_user();
echo 'Welcome <strong><a href="http://kolkataonwheelsmagazine.com/wp-admin/index.php" >'.$user->user_firstname.'</a></strong>
| <a href="http://kolkataonwheelsmagazine.com/wp-login.php?action=logout"">Logout</a>';
} else { ?>
<strong><?php wp_loginout(); ?></strong>
or <a href="<?php bloginfo('url') ?>/wp-login.php?action=register"> <strong>Register</strong></a>
<?php }?>
</li>
but problem is :
redirect to login page not current page...
I want it should go to home page after logout. Please help me...
Share Improve this question asked Jul 10, 2012 at 13:22 Gopal BhattacharjeeGopal Bhattacharjee 1632 gold badges4 silver badges11 bronze badges6 Answers
Reset to default 30add_action('wp_logout','auto_redirect_after_logout');
function auto_redirect_after_logout(){
wp_safe_redirect( home_url() );
exit;
}
This will do the trick.
If you only want to modify logout and not login, then use wp_logout_url(). Conversely you can use wp_login_url() for just the login URL.
Example of an logout link:
<a href="<?php echo wp_logout_url( home_url() ); ?>" title="Logout">Logout</a>
You can still use the function wp_loginout() but the redirect will work on both login and out. But if that is okay it could look like this:
<?php wp_loginout( home_url() ); ?>
Pontus Abrahamsson is right, that is a legal code, but here is another one that I'm using in my theme.
<a href="<?php echo wp_logout_url('$index.php'); ?>">Logout</a>
Since Wordpress 4.2.0
a logout hook filter was introduced logout_redirect
.
apply_filters( 'logout_redirect', string $redirect_to, string $requested_redirect_to, WP_User $user )
Filters the log out redirect URL.
- Source @ https://developer.wordpress.org/reference/hooks/logout_redirect/
Parameter | Description |
---|---|
$redirect_to |
(string ) The redirect destination URL. |
$requested_redirect_to |
(string ) The requested redirect destination URL passed as a parameter. |
$user |
(WP_User ) The WP_User object for the user that's logging out. |
Simple use case scenario
<?php
add_filter( 'logout_redirect', function() {
return esc_url( home_url() );
} ); ?>
Anonymous functions used, PHP >
5.3
required
Learn more
- The same apply for logins, with a
login_redirect
introduced in3.0.0
.
Since WordPress 4.2.0 you can just add:
add_filter('logout_redirect', 'get_home_url');
Try this
add_action('check_admin_referer', 'pfwp_logout_without_confirm', 10, 2);
function pfwp_logout_without_confirm($action, $result)
{
/**
* Allow logout without confirmation
*/
if ($action == "log-out" && !isset($_GET['_wpnonce'])) {
$redirect_to = isset($_REQUEST['redirect_to']) ? $_REQUEST['redirect_to'] : get_permalink( wp_logout_url() )// or custom url;
$location = str_replace('&', '&', wp_logout_url($redirect_to));
header("Location: $location");
die;
}
}
本文标签: Logout redirect to home page
版权声明:本文标题:Logout redirect to home page 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738651227a2104882.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论