Zax Rosenberg, CFA
  • Home
  • Blog
  • Contact
  • Privacy
GitHub
LinkedIn
  • Privacy
GitHub
LinkedIn
Subscribe
Zax Rosenberg, CFA
Zax Rosenberg
  • Home
  • Blog
  • Contact
  • Technology

Dynamic Login/Logout Menu Buttons for WordPress

  • byZax
  • Posted on May 12, 2016March 10, 2017
  • 2 minute read
  • 10K views
Dynamic Login/Logout Menu Buttons for WordPress
  • byZax
  • Posted on May 12, 2016March 10, 2017

The login/logout functions in WordPress are great, but what if you don’t want to have to use a sidebar widget to login/out?  My latest website didn’t even HAVE a sidebar, but I still wanted to make sure the login/logout was prominently displayed.  The code below will allow you to add a button to your menus (code below shows option to include on all menus, or just the “primary” menu) that shows “Log In/Register” for users who aren’t currently logged in, and then switch to a “Log Out” button when they are:

In functions.php (anywhere, but I put it at the end), add the following:

PHP
1
2
3
4
5
6
7
8
9
10
11
/************* Log In/Register and Log Out in Menu ****************/
add_filter( 'wp_nav_menu_items', 'rkk_add_auth_links', 10 , 2 );
function rkk_add_auth_links( $items, $args ) {
if ( is_user_logged_in() ) {
$items .= '<li><a href="'. wp_logout_url() .'">Log Out</a></li>';
}
elseif ( !is_user_logged_in() ) {
$items .= '<li><a href="'. site_url('wp-login.php') .'">Log In/Register</a></li>';
}
return $items;
}

To have the buttons show up on the Primary menu ONLY, replace both:

PHP
1
if ( is_user_logged_in() ) {

sections with:

PHP
1
if ( is_user_logged_in() && $args->theme_location == 'primary') {

If you’d rather have different buttons for the Log in, Register, Lost Password and Log Out pages, use the following code instead:

PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
/************* Login, Register, Lost Password, and Log Out in Menu****************/
add_filter( 'wp_nav_menu_items', 'rkk_add_auth_links', 10 , 2 );
function rkk_add_auth_links( $items, $args ) {
if ( is_user_logged_in() ) {
$items .= '<li><a href="'. wp_logout_url() .'">Log Out</a></li>';
}
elseif ( !is_user_logged_in() ) {
$items .= '<li><a href="'. site_url('wp-login.php') .'">Log In/Register</a></li>';
$items .= '<li><a href="'. site_url('wp-login.php?action=register') .'">Register</a></li>';
$items .= '<li><a href="'. site_url('wp-login.php?action=lostpassword') .'">Lost Password</a></li>';
}
return $items;
}

 

Related Topics
  • Login Button
  • PHP
  • WordPress
Previous Article
  • Technology

Handy WooCommerce Code

  • byZax
  • Posted on April 12, 2016March 10, 2017
View Post
Next Article
  • Technology

Best Free Themes and Plugins for a bbpress Forum (2016)

  • byZax
  • Posted on May 12, 2016March 10, 2017
View Post

1 comment

  1. Belarmino says:
    July 25, 2020 at 2:12 pm

    Can you explain how to use a dropdown menus to show register and lost password

    Reply

Leave a Reply Cancel reply

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

Trending Posts
  • Crohn’s Cooking: 10 spicy flavors that aren’t from capsicum peppers
    • Posted on 12.07.17March 9, 2018
    • Zax
    • 3
    • 31.6K
  • How to Write a Runescape Auto Clicker with Python, Part I
    • Posted on 09.05.17January 2, 2018
    • Zax
    • 7
    • 31.3K
  • How to Write a Runescape Autoclicker with Python, Part II
    • Posted on 09.05.17July 3, 2017
    • Zax
    • 12
    • 23.9K
Copyright © 2021 Zax Rosenberg. All rights reserved.

Input your search keywords and press Enter.