admin管理员组文章数量:1328589
How can I add a link to the primary navigation menu with the class="right"
attribute?
I tried to add a static link to example/wp-logout.php?action=logout
but that leads to a logout confirmation page. Is there any way to make it a log out link?
How can I add a link to the primary navigation menu with the class="right"
attribute?
I tried to add a static link to example/wp-logout.php?action=logout
but that leads to a logout confirmation page. Is there any way to make it a log out link?
4 Answers
Reset to default 49You can achieve this using the wp_nav_menu_items
hook. Let's have a look at the following piece of code which shows the login/logout link on the primary
menu location.
add_filter( 'wp_nav_menu_items', 'wti_loginout_menu_link', 10, 2 );
function wti_loginout_menu_link( $items, $args ) {
if ($args->theme_location == 'primary') {
if (is_user_logged_in()) {
$items .= '<li class="right"><a href="'. wp_logout_url() .'">'. __("Log Out") .'</a></li>';
} else {
$items .= '<li class="right"><a href="'. wp_login_url(get_permalink()) .'">'. __("Log In") .'</a></li>';
}
}
return $items;
}
This is what we have implemented in the above example.
- First added a filter for
wp_nav_menu_items
hook and attached a function to it. - After checking for
primary
theme location, we have checked whether user is logged in or not. - If logged in, we have showed the
Log Out
link otherwise theLog In
link. - We have passed the permalink of the currently viewing page to the login url so that user will be redirected to the current page after successful login.
- We have used the
class="right"
to the above code to meet your requirement.
You can find a detailed explanation on this blog.
Try adding a custom link with http://example/wp-login.php?action=logout It worked for me!
If you're flexible about adding a plugin to get this functionality, you could use: https://wordpress/plugins/login-logout-register-menu/
It simply adds a very convenient section in the menu builder. You can combine it with another plugin to restrict what menu items are shown to logged in users, which ones to logged out users, and which ones to everyone.
My footer menu is a widget, therefore I had difficulties using the code by Chittaranjan. The following edited version works for me. I also changed the links and made them "dynamic": login leads to a page of your choice, logout will either stay on the current page, or send to home, if the current page is your (private) login page. Ideally it would check if your login page is actually private, but I don't know how to do that, sorry.
add_filter( 'wp_nav_menu_items', 'wti_loginout_menu_link', 10, 2 );
function wti_loginout_menu_link( $items, $args ) {
//var_dump($args);
if (($args->menu->slug == 'footer')) {
if (is_user_logged_in()) {
$loginlink = '/your-private-page';
$logoutlink = get_permalink();
if (strpos($logoutlink, $loginlink) !== false) {
$logoutlink = '/';
}
$items .= '<li class="right"><a href="'. wp_logout_url($logoutlink) .'">'. __("Log Out") .'</a></li>';
} else {
$items .= '<li class="right"><a href="'. wp_login_url($loginlink) .'">'. __("Log In") .'</a></li>';
}
}
return $items;
}
本文标签: loginAdd quotLogoutquot link to navigation menu
版权声明:本文标题:login - Add "Logout" link to navigation menu 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742257113a2441828.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
wp_loginout()
? – kaiser Commented Jul 30, 2014 at 16:18