Support » Fixing WordPress » Add a log out button to the menu

  • Hi, For one day now, I am trying to add a logout button to my website (local) but those codes that I found on the internet are not working.
    I was thinking about creating my own PHP file to log out member, but I don’t know how to do it
    Can anybody help me, please?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Add this code on function.php and define a name of a menu on code.

    <?php
    add_filter('wp_nav_menu_items','add_search_box', 10, 2);
    function add_search_box($items, $args) {
        if($args->menu=="top_menu"): // add your menu name
            ob_start();
            if(is_user_logged_in()):
                $items.= '<li><span class="my-account"><i class="fa fa-lock"></i><a href="'.wp_logout_url().'">Logout</a></span></li>';
            endif;
        endif;
        return $items;
    }
    ?>

    If you are able to add the logout button then use the function “wp_logout_url()” to logout the user from the site.

    Ex: echo “Logout“;

    Thread Starter princelionelnzi

    (@princelionelnzi)

    Hi @ravi, thanks for replying. I tried you code and by changing “top_menu” by my menu named “Main Menu” which is also the primary menu; (I also tried it by replacing “top_menu” by “primary” and “MainMenu” but it did not work).

    Can you kindly write the code again with the appropriate menu name based on the information given above?

    Thanks for your help.

    I use this code on multiple sites for a LOGIN menu item which changes to a logout menu item when the user is logged in:

    function add_loginlogout_link($items, $args)
    {
    	//defaults; change this as you see fit
    	$defaults = array(
    		'loginlout_position' => 999, //enter 0 for front, 999 for end
    		'menu_id' => 'menu-item', //custom CSS
    		'menu_class' => 'menu-item menu-item-type-custom menu-item-object-custom', //custom CSS
    		'menu_location' =>'main-menu', //enter primary, secondary, etc.
    		'loginredirect' => false //enter true for redirect to wp-admin dashboard
    	);
    
    	//do nothing if not proper menu location
    	if( $args->theme_location != $defaults['menu_location'] )
    		return $items;
    
    	//set redirect URL
    	if( $defaults['loginredirect'] )
    		$wpurl = 'yourpage/'; //USE YOUR PAGE SLUG HERE
    	else
    		$wpurl = 'wp-admin/index.php';
    
    	// split the menu items into an array using the ending <li> tag
    	if ( $defaults['loginlout_position'] != 0 && $defaults['loginlout_position'] != 1 && $defaults['loginlout_position'] != 999 ) {
    		$items = explode('</li>',$items);
    	}
    
    	if(is_user_logged_in())
    	{
    		$newitem = '<li id="'.$defaults['menu_id'].'" class="'.$defaults['menu_class'].'"><a title="Logout" href="'. wp_logout_url($wpurl) .'">Logout</a></li>';
    		if ( $defaults['loginlout_position'] == 0 || $defaults['loginlout_position'] == 1 )
    			$newitems = $newitem.$items;
    		elseif ( $defaults['loginlout_position'] == 999 )
    			$newitems = $items.$newitem;
    		else
    			$newitem = '<li id="'.$defaults['menu_id'].'" class="'.$defaults['menu_class'].'">' . $args->before . '<a title="Logout" href="'. wp_logout_url('index.php') .'">' . $args->link_before . 'Logout' . $args->link_after . '</a>' . $args->after; // no </li> needed this is added later
    	}
    	else
    	{
    		$newitem = '<li id="'.$defaults['menu_id'].'" class="'.$defaults['menu_class'].'"><a title="Login" href="'. wp_login_url($wpurl) .'">Login</a></li>';
    		if ( $defaults['loginlout_position'] == 0 || $defaults['loginlout_position'] == 1 )
    			$newitems = $newitem.$items;
    		elseif ( $defaults['loginlout_position'] == 999 )
    			$newitems = $items.$newitem;
    		else
    			$newitem = '<li id="'.$defaults['menu_id'].'" class="'.$defaults['menu_class'].'">' . $args->before . '<a title="Login" href="'. wp_login_url('index.php') .'">' . $args->link_before . 'Login' . $args->link_after . '</a>' . $args->after; // no </li> needed this is added later
    	}
    
    	if ( $defaults['loginlout_position'] != 0 && $defaults['loginlout_position'] != 1 && $defaults['loginlout_position'] != 999 ) {
    		$newitems = array();
    
    		// loop through the menu items, and add the new link at the right position
    		foreach($items as $index => $item)
    		{
    		// array indexes are always one less than the position (1st item is index 0)
    			if($index == $defaults['loginlout_position']-1)
    			{
    				$newitems[] = $newitem;
    			}
    			$newitems[] = $item;
    		}
    
    		// finally put all the menu items back together into a string using the ending <li> tag and return
    		$newitems = implode('</li>',$newitems);
    	}
    
    	return $newitems;
    }
    add_filter('wp_nav_menu_items', 'add_loginlogout_link', 10, 2);

    Make note of the comments in above.

    add_filter('wp_nav_menu_items','add_logout_link', 10, 2);
    function add_logout_link($items, $args) {
        // print_r($args); exit;
        if (is_user_logged_in() && $args->theme_location == 'primary'):
            ob_start();
                $items.= '<li><span class="my-account"><i class="fa fa-lock"></i><a href="'.wp_logout_url().'">Logout</a></span></li>';
        endif;
        return $items;
    }

    After this code add not work please check this function with remove comment before print_r() check a argument value of your menu.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Add a log out button to the menu’ is closed to new replies.