WP Smith

Creating WordPress & Genesis Websites Since 2010

  • Home
  • About
  • Services
  • Blog
  • Contact

May 11 2011

How to Add a Login/Logout Link to Your WordPress Genesis Custom Menu Based on Menu Location

So one of the things I was looking into was how to add a login/logout link to th This e Navigation. However, when I first did it, I did it to all of them; however, I finally figured out how to create this link, as seen in the footer, in the menu based on the menu. This tutorial targets Genesis users but non-Genesis users can use the same code with the deletion of the last line.

All Genesis themes have Primary and Secondary menus registered in the Genesis engine.
{DO NOT ADD THIS CODE ANYWHERE: DEMO ONLY}
[php]<?php
register_nav_menus( array(
'primary' => __('Primary Navigation Menu', 'genesis'),
'secondary' => __('Secondary Navigation Menu', 'genesis')
) );
?>[/php]

As you can see the two names are primary and secondary in lower case. One other Genesis child theme, News, registers a menu called topnav.

You will need to add the following to your functions.php file based on where you want the link to occur.

[php highlight="4,5,6,7,8,9,10,11"]
<?php
function wps_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' =>'primary', //enter primary, secondary, or topnav for News
'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 = 'wp-admin/index.php';
else
$wpurl = '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', 'wps_add_loginlogout_link', 10, 2);
add_filter('genesis_nav_items', 'wps_add_loginlogout_link', 10, 2); //non-Genesis users delete this line.
?>
[/php]

Written by Travis Smith · Categorized: Genesis, Tutorials

StudioPress Premium WordPress Themes     WP Engine Managed WordPress Hosting

What can I do for you!?

Custom Development

We develop plugins by determining both business/functional and technical requirements, following WordPress development best practices, and using agile methodology to ensure you get the best solution.

Consulting

Have questions? Need a reliable developer to consult? Please contact us today!

Customized Theme

We can customize your theme or child theme, or create a child theme for you based on your needs while enhancing the performance of every individual attribute.

Customized Plugin

We can customize your plugins, extend plugins (e.g., Gravity Forms, Jetpack, Soliloquy) based on your needs ensuring security, performance, and positive business impact.

Contact Us

About Travis Smith

As a WordPress enthusiast, developer, and speaker, Travis writes about what he learns in WordPress trying to help other WordPress travelers, beginners and enthusiasts with tutorials, explanations, & demonstrations.

Comments

  1. Bruce says

    May 12, 2011 at 8:58 am

    Very nice!
    I’ve been looking for a way to do this. The feature to add it in the desired position is great. This must have taken some time to come up with. Nice job!

    Reply
  2. Mark Sullivan says

    August 4, 2011 at 2:59 pm

    Thanks for this.

    Spent at least 2 hours searching for some proper help on this. You nailed it.

    Might be worth pointing out to other noobs, that the default menu name must be changed as per the registered menu names. Caught me out for 10mins or so. Seems so obvious now.

    Thanks

    Reply
  3. Ali says

    August 19, 2011 at 5:48 am

    this sounds promising but i couldn’t do it i don’t know why!
    i added this piece of code in the functions.php file but nothing happened… i don’t know what exactly should be edited here too. can you please explain more?
    i’ll be very thankful

    Reply
    • wpsmith says

      August 19, 2011 at 9:03 am

      Hello Ali,

      Simply set the defaults (lines 4-11) to whatever you’d like. Please let me know if you need anything else.

      Thanks,

      Travis

      Reply
  4. Efrem says

    September 30, 2011 at 12:58 pm

    Ditto what Mark said above on all counts, but mostly, thank you for this great code.

    I too spent the last two hours searching the Studio Press forums and Google trying to find something that worked this well on a single nav menu instead of having the link duplicated on the primary and secondary nav.

    -Efrem

    Reply
  5. hamza khan says

    February 25, 2012 at 7:33 am

    thanks you so much !!

    Reply
  6. Fouad says

    April 7, 2012 at 4:29 pm

    hey mate great code! many thanks!
    but i have a question..
    im using profile builder script which give me shortcodes to add login/register/edit profile forms in a page i make.. so my question is can i make the login button goes to the page i created with the login form ? instead of going to wp-login.php the wordpress login page?
    Thanks Again!

    Reply
    • Fouad says

      April 7, 2012 at 5:52 pm

      i changed in line 40
      $newitem = ‘Login‘;
      and it worked perfect on wamp when i tried it
      but when i added it to my website function.php i get this error while cliclink on Login
      Warning: Cannot modify header information – headers already sent by (output started at /home/fudcom/public_html/wp-content/themes/duotive-fortune/functions.php:25) in /home/fudcom/public_html/wp-login.php on line 349

      Warning: Cannot modify header information – headers already sent by (output started at /home/fudcom/public_html/wp-content/themes/duotive-fortune/functions.php:25) in /home/fudcom/public_html/wp-login.php on line 361

      Pffffff thats so weird ! it works on my wamp but not on the website and i have the same plugins installed
      do u have any idea ?:(

      Reply
      • Fouad says

        April 7, 2012 at 5:55 pm

        sorry the code i posted didnt appear… i changed on the line 40 this thing inside the href ‘. wp_login_url($wpurl) .’ and replaced it with my page link and worked perfect on wamp! but not on website :/

        Reply
        • Luis says

          January 4, 2016 at 7:07 pm

          I got the same error. Could you solve it? How?

          Reply
    • Travis Smith says

      April 23, 2012 at 2:54 pm

      Yes you can, and this code is quite beyond what you need.

      Reply
  7. Dallas Peters says

    April 23, 2012 at 2:04 pm

    With this code is it possible to insert an array for the ‘menu_location’ =>’utility_navigation’ to have it spit out the same code for inserting in two locations?

    I’m serving up two different menus for logged in and logged out users but would love for this link to show up in both!

    Reply
    • Travis Smith says

      April 23, 2012 at 2:54 pm

      Yes, change line 9 to:
      [php]
      ‘menu_location’ => array( ‘primary’, ‘secondary’ ), //change to whatever your menu ids are
      [/php]

      And then line 14 to:
      [php]
      if( in_array( $args->theme_location, $defaults[‘menu_location’] ) )
      [/php]

      Reply
      • David A. Pimentel says

        May 19, 2012 at 5:17 pm

        Actually, line 14 should be
        if( ! in_array( $args->theme_location, $defaults[‘menu_location’] ) )
        Otherwise, the feature will be missing from the desired menus.
        Thanks for code. It works very well.

        Reply
  8. Jonathan says

    March 14, 2013 at 3:35 pm

    Thanks Travis! Really helpful.

    Reply
  9. Andy Freist says

    October 30, 2013 at 9:21 pm

    Can this be used to work for a menu that is in the Custom Menu Widget? Trying to get this to work for the header nav (which uses the widget) in the Minimum Pro genesis theme..

    Any insight would be appreciated!

    Reply
    • Travis Smith says

      February 11, 2014 at 10:31 am

      Yes but the Custom Menu Widget sadly does not have a location, which it should in my opinion. Nonetheless, it can but may require some fancy checking. Please let me know if you need more help on this.

      Reply
  10. Martin Haranta says

    November 22, 2013 at 11:27 am

    Hi, great job with the code, i have just one question how can i remove this login/logout from several particular pages? e.g. i have it on my secondary navigation but i want to be show only on six pages from this secondary navigation. How can i do that?

    Thanks

    Reply
  11. Martin Haranta says

    November 25, 2013 at 5:07 am

    Hi, great job with the code, i have just one question how can i remove this login/logout from several particular pages? e.g. i have it on my secondary navigation but i want to be show only on six pages from this secondary navigation. How can i do that? –

    Thanks

    Reply
  12. Laza says

    May 28, 2014 at 3:42 pm

    Great tuts however, I think that you should start using pasbin/git or some way to better show off your code.

    Reply
  13. mani says

    February 27, 2015 at 7:07 am

    hi i cant find good solution to add login logout menu for membership in my website…could you help me

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  • Twitter
  • Facebook
  • LinkedIn
  • Google+
  • RSS

Copyright © 2025 � WP Smith on Genesis on Genesis Framework � WordPress � Log in