Forums

Login/Logout in Menu (4 posts)

  1. Wasca
    Member
    Posted 8 months ago #

    Hi Guys

    I have the following code in my functions.php for adding a login/logout to my menus

    add_filter('wp_nav_menu_items', 'add_login_logout_link', 10, 2);
    function add_login_logout_link($items, $args) {
    
    ob_start();
    wp_loginout('index.php');
    $loginoutlink = ob_get_contents();
    ob_end_clean();
    
    $items .= '<li>'. $loginoutlink .'</li>';
    
    return $items;
    }

    I need to change the code so it only applies to my secondary menu that is registered like so.

    function register_main_menus() {
    	register_nav_menus(
    		array(
    			'primary-menu' => __( 'Primary Menu' ),
    			'secondary-menu' => __( 'Second Menu' )
    		)
    	);
    }

    How can I apply this login/logout code to just the Secondary Menu?

    Part 2...
    I'd also like to be able to select the order it is placed in. Right now I need it added to the end of the menu items.

    Thanks

  2. Wasca
    Member
    Posted 8 months ago #

    ok I was able to get it to just display on my second menu by doing this

    add_filter('wp_nav_menu_items', 'add_login_logout_link', 10, 2);
    function add_login_logout_link($items, $args) {
    		ob_start();
    		wp_loginout('index.php');
    		$loginoutlink = ob_get_contents();
    		ob_end_clean();
    		if( $args->theme_location == 'secondary-menu' ){
    			$items .= '<li>'. $loginoutlink .'</li>';
    		}
    		return $items;
    }

    Now how do I position it in the menu?

  3. InterMike
    Member
    Posted 8 months ago #

    I'd also like to know how. Tried changing the priority number 10 to something else, but it didn't work.

  4. Svitlana21
    Member
    Posted 2 weeks ago #

    No extra coding is needed. Here is easy to use plugin - BAW Login/Logout menu:
    http://wordpress.org/extend/plugins/baw-login-logout-menu/

Reply

You must log in to post.

About this Topic