Add Login / Logout to Primary Menu


/ Published in: PHP
Save to your folder(s)

Adds a Logout link to the end of your Primary Navigation is the user is logged in; else adds a Login link to the end of your Primary Navigation.


Copy this code and paste it in your HTML
  1. add_filter( 'wp_nav_menu_items', 'add_login_logout_to_primary_menu', 10, 2 );
  2. function add_login_logout_to_primary_menu( $items, $args ) {
  3. if( 'primary' === $args->theme_location ) :
  4.  
  5. if (is_user_logged_in()) {
  6. $items .= '<li><a href="'.wp_logout_url(home_url()).'">Logout</a></li>';
  7. } else {
  8. $items .= '<li><a href="'.wp_login_url().'">Login</a></li>';
  9. }
  10.  
  11. endif;
  12. return $items;
  13. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.