Skip to content

Instantly share code, notes, and snippets.

@nickcernis
Last active May 26, 2018 19:18
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nickcernis/02086c75ee9aa19447e0 to your computer and use it in GitHub Desktop.
Save nickcernis/02086c75ee9aa19447e0 to your computer and use it in GitHub Desktop.
Add a login/logout link to the menu bar of any Genesis child theme
<?php // add everything except for this opening line to your functions file
add_filter( 'wp_nav_menu_items', 'sp_add_loginout_link', 10, 2 );
function sp_add_loginout_link( $items, $args ) {
// Change 'primary' to 'secondary' to put the login link in your secondary nav bar.
if ( $args->theme_location != 'primary' ) {
return $items;
}
if ( is_user_logged_in() ) {
$items .= '<li class="menu-item"><a href="' . wp_logout_url( home_url() ) . '">Log Out</a></li>';
} else {
$items .= '<li class="menu-item"><a href="' . site_url( 'wp-login.php' ) . '">Log In</a></li>';
}
return $items;
}
@registar
Copy link

I can't thank you enough for this. It's saved me a lot of time and energy trying to find a solution while using MemberMouse and the Altitude Theme from StudioPress. Thanks Nick!

@mnschwarz
Copy link

this works perfectly for my primary menu and even a custom third menu I set up. But I cant get it to work for the secondary menu and it's driving me insane. Any ideas why it wouldnt work just for this one location? the theme location is definitely secondary....

@sixstringseraph
Copy link

Thanks so much for this! Using it taught me quite a bit on the genesis customization. I have one question though. How would you make it so the added item is listed first rather than appended to the end?

@nickcernis
Copy link
Author

nickcernis commented Jul 15, 2016

@sixstringseraph Glad it was helpful.

You could use this format to prepend the link to existing menu items instead of appending it:

    if ( is_user_logged_in() ) {
        $items = '<li class="menu-item"><a href="'. wp_logout_url() .'">Log Out</a></li>' . $items;
    } else {
        $items = '<li class="menu-item"><a href="'. site_url('wp-login.php') .'">Log In</a></li>' . $items';
    }

The original line:

$items .= '<li class="menu-item"><a href="'. wp_logout_url() .'">Log Out</a></li>';

is shorthand for:

$items = $items . '<li class="menu-item"><a href="'. wp_logout_url() .'">Log Out</a></li>';

To reverse them, you swap the $items and the new menu link HTML.

@AmeliaBriscoe
Copy link

Hey Nick! Thanks for the code snippet :-)

I ended up adding a redirect to my code so members were redirected to my member dashboard when logging in, and then the home page when logging out.

if ( is_user_logged_in() ) {
    $items = $items . '<li class="menu-item"><a href="'. wp_logout_url( home_url() ) .'">Log Out</a></li>';
} else {
    $items = $items . '<li class="menu-item"><a href="'. site_url('wp-login.php/?redirect_to='. home_url( '/member-dashboard/')) .'">Log In</a></li>';
}

I used home_url for the redirect, rather than site_url, as I have a sub folder going on in my set up.

I'm sure it could be extended to redirect to the permalink the member was on or to any of the other interesting places on my website!

@nickcernis
Copy link
Author

Nice addition, @MissAmeliaSmith! Thanks for sharing it here.

@braddalton
Copy link

Here's another way to do it wp_loginout https://codex.wordpress.org/Function_Reference/wp_loginout

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment