How To Add a login/logout link to Your Navigation Menu

  • 8 comments
  • 8 Comments
    Posted 25 Jan 2011 in WordPress Tricks

    New WordPress 3 Menus is Awesome feature, I really like it, using WordPress Navigation Menu, we can create more sophisticated menu ever seen. We can arrange our menu more easily and many more. Now we will share How To Add a login/logout link to your navigation menu.

    You need to write this code on your functions.php and the login/logout link will be automatically added on your WordPress Menu Navigation

    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;
    }

    First we add a function add_login_logout_link to the wp_nav_menu_items filter. Then, the ob_start, ob_get_contents and ob_end_clean (lines 4, 6 and 7) functions are output Buffering PHP functions that will intercept the information that would otherwise be sent to the browser. wp_loginout(‘index.php’); will add the logic and html code to login (if not logged in yet), or logout (if logged in). Since we don’t want to send that code to the browser yet, we capture the output (using ob_get_contents) in a variable ($searchform), and finally include that variable as a list item in the menu. via VanWeerd

    Resource
    http://codex.wordpress.org/Navigation_Menus


    7 Comments

    1. Ashkir

      Thank you!

      Reply
    2. James

      Tried to add this but came back with lots of errors in your code above. Removed all the additional formatting and it still didn’t work. Heres the code I’m using

      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 .= ”. $loginoutlink .”;

      return $items;
      }

      Reply
    3. Michael

      Thank you 🙂

      Your script works great for me, and I’ve added it to my site:
      http://www.buildinganonline.com/

      Best regards
      Michael

      Reply
      • Michael

        Hello again,

        Now I was just so happy, but I have just discovered that the (great) link now also appears in my category meny!

        What the #¤&% is it doing there and how do I get it to appear only in my page menu ???

        Please help out thank you.

        Best regards
        Michael

        🙂

        Reply
        • Michael

          As you will see if you visit my site, I have removed the function, as it makes the login/logout appears in every menu bar you may have activated on your theme.

          Usually page menu and category menu bars.

          And actually there is a much smarter and much more easy way to solve this matter:

          http://codex.wordpress.org/MU_Theme_Login

          It’ll work in almost all themes but sadly not mine 🙁

          Michael

          Reply
    4. zalila

      i want to add my custom code with if cond… how to add it ?

      Reply
    5. joanne

      I have been wanting to know how to add all kinds of things to the main menu with a function.

      Thanks! I hope this works with javascript! Can’t wait to give it a try…

      Reply


    Your Comment to Ashkir

    Enter your comment*:

    * Required fields