User Registration

  1. Home
  2. Docs
  3. User Registration
  4. How to?
  5. How to add login/logout link in navigation menu programatically

How to add login/logout link in navigation menu programatically

To add login/logout link to the menu add the following code to your theme’s function.php. The codes will be wiped out with a theme update. So, you need to create a child theme and add there. Here is the tutorial for that: https://themegrill.com/blog/tutorial-creating-wordpress-child-theme/ and add to your child theme’s function.php

 

add_filter( 'wp_nav_menu_items', 'ur_add_loginout_link', 10, 2 );

function ur_add_loginout_link( $items, $args ) {
     if (is_user_logged_in() && $args->theme_location == 'primary') {
           $items .= '<li><a href="'. ur_logout_url( get_permalink( ur_get_page_id( 'myaccount' ) ) ) .'">Log Out</a></li>';
      } elseif ( ! is_user_logged_in() && $args->theme_location == 'primary' ) {
           $items .= '<li><a href="' . get_permalink( ur_get_page_id( 'myaccount' ) ) . '">Log In</a></li>';
      }
       return $items;
}
Was this article helpful to you? Yes 2 No 3

How can we help?