• Francesco

    (@francesco_5005)


    Hi,

    I want to use the tecnique described here: https://www.datafeedr.com/dynamically-add-link-to-wordpress-menu-based-on-users-logged-in-status/ to add the login and logout links in the header

    /**
    * Adds a link to the Primary Menu based on the
    * user's logged in status. This is to be placed
    * in your theme's functions.php file or in your
    * own plugin file.
    *
    * This example works for the WordPress Twenty-* Twelve theme.
    */
    
    function twentytwelve_wp_nav_menu_items($items, $args) {
    
    // Make sure this is the Primary Menu.
    // You may need to modify this condition
    // depending on your theme.
    
    if ($args->theme_location == 'primary') {
    
    // CSS class to use for <li> item.
    
    	$class = 'menu-item';
    
    	if (is_user_logged_in()) {
    
    // User is logged in, link to welcome page.
    
    		$extra = '<li id="menu-item-logged-in-user" class="'.$class.'"><a href="/welcome">'.__('Welcome').', '.wp_get_current_user()->user_login.'!</a></li>';
    	} 	
    
    	else {
    
    // User is guest, link to login page.
    
    		$extra = '<li id="menu-item-logged-out-user" class="'.$class.'"><a href="/wp-login.php">'.__('Log in').'</a></li>';
    	}
    
    // Add extra link to existing menu.
    	$items = $items . $extra;
    }
    
    // Return menu items.
    
    return $items;
    
    }
    
    // Hook into wp_nav_menu_items.
    
    add_filter( 'wp_nav_menu_items', 'twentytwelve_wp_nav_menu_items', 10, 2 );

    to me the main problem it’s here ($args->theme_location == 'primary')

    I tried to use “Main Menu” as theme_location but it didn’t worked.

    Do you have any suggestion?

  • The topic ‘login and logout links in the header’ is closed to new replies.