Simple WordPress “Loginout or Register” Menu

Simple Loginout Menu

A simple, very lightweight login or logout menu that also adds an option for logged-out users to visit a registration/sign-up page. At some point we can follow up with a much more advanced and complicated approach involving re-direction and javascript.

 

/* LOGINOUT MENU */
add_filter( 'wp_nav_menu_items', 'add_login_logout_register_menu', 199, 2 );

function add_login_logout_register_menu( $items, $args ) {
  
  if ( $args->theme_location != 'secondary-menu' ) {
    
    return $items;
    
  }

  if ( is_user_logged_in() ) {
    
      $items .= '
      <li>
        <a href="' . wp_logout_url() . '">' . __( 'Log Out' ) . '</a>
      </li>
      ';
      
  } else {
        
      $items .= ' 
      <li>
        <a id="join-wmhca" href="/join-wmhca/">' . __( 'Join WMHCA' ) . '</a>
      </li>
      ';
      
      $items .= '
      <li>
        <a id="loginout" href="/wp-admin/">' . __( 'Log In' ) . '</a>
      </li>
      ';
      
  }

  return $items;
  
}

 

 

Leave a Reply