admin管理员组

文章数量:1328379

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?

Share Improve this question asked Jul 29, 2014 at 18:54 Zach RussellZach Russell 1,0933 gold badges15 silver badges33 bronze badges 6
  • 1 Have you seen existing questions and answers like this one? – fuxia Commented Jul 29, 2014 at 19:04
  • Have you seen wp_loginout()? – kaiser Commented Jul 30, 2014 at 16:18
  • 2 I have removed the accepted answer, because it was plagiarized from this site without attribution. – fuxia Commented Jul 30, 2014 at 23:13
  • I ended up using something similar; at least using the loginout() function. I'll post exactly what I did next time I'm in front of my computer. Thanks @toscho – Zach Russell Commented Jul 31, 2014 at 2:30
  • You can use this free plugin wordpress/plugins/login-logout-register-menu to achieve the same easily. – Vinod Dalvi Commented Apr 16, 2017 at 7:40
 |  Show 1 more comment

4 Answers 4

Reset to default 49

You 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.

  1. First added a filter for wp_nav_menu_items hook and attached a function to it.
  2. After checking for primary theme location, we have checked whether user is logged in or not.
  3. If logged in, we have showed the Log Out link otherwise the Log In link.
  4. 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.
  5. 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