Adding a login/logout link to any WP menu is very easy using the wp_nav_menu_items hook. In the Chamber Dashboard Member Manager plugin, we wanted to give our users an option to add the login/logout link to the selected menu of their choice. 

There was an option to enable the login/logout link which would add the link to all the available menu’s on the site, but this was not ideal. So, I added another option where the admins can select the menu they want the link to be added to. 

There are a couple of steps involved in this process.  

Displaying the list of available menus

The first step was to display a list of registered nav menus’ on the site using get_registered_nav_menus() and display them as checkboxes. 

function selected_menu_render($args){ 
    $options = get_option( 'plugin_options' ); 
    if(isset($options['selected_menu'])){ 
        $selected_menu = $options['selected_menu']; 
    }else{ 
        $selected_menu = array(); //This is important because if there are no menu's selected, we want to initialize it to an empty array instead of a string. 
    } 

    $nav_menus = get_registered_nav_menus(); //gets the registered menu's on the site 

//Adding name="plugin_options[selected_menu][] - this is important because, if we don't mention it as an array, multiple checkbox values will not be stored

//checked( in_array( $menu_name, $selected_menu ), 1 ) - This checks to see if the menu name is in the selected_menu array and if yes, the box is checked

    if(!empty($nav_menus) && $nav_menus != ''){
        foreach($nav_menus as $menu_name => $menu){
        ?>
        <input id="cdashmm_selected_menu" type="checkbox" class="cdashmm_selected_menu" name="cdashmm_options[cdashmm_selected_menu][]" value=<?php echo $menu_name; ?> <?php checked( in_array( $menu_name, $cdashmm_selected_menu ), 1 ); ?> /><label name="cdashmm_selected_menu_label"><?php echo $menu; ?></label><br />
        <?php
        }
        ?>
        <br />
        <span class="description"><?php echo $args[0]; ?></span>
        <?php
    }
} 
List of available WordPress Menus

How to add custom posts in WordPress using Ajax

The second step is to check which menu location has been selected and add the login/logout link to that particular location.

function cdashmm_add_login_logout_link($items, $args){ 
  $site_url = cdashmm_get_login_page_url(); //getting the login page url that has been set in the options page. 
  $options = get_option( 'cdashmm_options' ); //getting the plugin options 

  if(isset($options['cdashmm_selected_menu'])){ 
        $selected_menu_locations = $options['cdashmm_selected_menu']; 
  }else{ 
      $selected_menu_locations = ''; 
  } 

  if(cdashmm_enable_login_logout_link()){ //checking to see if the login/logout item needs to be added to the menu 

      if($site_url !=''){ //We wanted to add the link only if the login page url is set in the options page 

          if(isset($selected_menu_locations) && is_array($selected_menu_locations) && $selected_menu_locations != '') { 
              foreach($selected_menu_locations as $location){ 
                 $menu_location = $location; 
                  if( $args->theme_location == $menu_location ){ 
                      $items .= cdashmm_login_logout_link($site_url); 
                  } 
              } 
          }else{ 
              $items .= cdashmm_login_logout_link($site_url); 
          } 
      } 
  } 
  return $items; 
} 

Here is the function that actually adds the link based on whether the user is logged in or not.

function cdashmm_login_logout_link($site_url){ 
    if(!isset($items)){ 
        $items = ''; 
    } 

    if ( cdashmm_is_user_logged_in() ){ 
       $items .= '<li class="right cdashmm_login_logout_link"><a href="'. wp_logout_url('index.php') .'">'. __("Log Out") .'</a></li>'; 
    } else { 
       $items .= '<li class="right cdashmm_login_logout_link"><a href="'. $site_url .'">'. __("Log In") .'</a></li>'; 
    } 
    return $items; 
} 
add_filter('wp_nav_menu_items', 'cdashmm_add_login_logout_link', 10, 2); 
Logout link added to the primary WordPress menu

Get tips and tutorials on using the new block editor, WordPress themes and plugins!

Leave a Reply

Leave a Reply

%d bloggers like this: