max-kk
12/27/2016 - 3:40 PM

Wordpress :: Add a login/register link if user not logged in OR Logout if Logged in

Wordpress :: Add a login/register link if user not logged in OR Logout if Logged in

<?php
/**
 * modify menu
 *
 * @param string  $items The menu items
 * @param object  $args 
 * 
 * @return string (maybe) modified items
 */
function modify_menu56484( $items, $args ) {
    
    $html = '';
    if( $args->theme_location == 'primary' ) {
  		if ( !get_current_user_id() ) {
  			//$menu_url = get_author_posts_url( get_current_user_id(), get_the_author_meta( 'user_nicename', get_current_user_id() ) );
  			$menu_title_login = '<span class="glyphicon glyphicon-lock"></span> Login';
  			$html .= '<li class="menu-item"><a href="' . wp_login_url() . '" class="menu-item-go-login">' . $menu_title_login . '</a></li>';
  			
  			$menu_title_reg = '<span class="glyphicon glyphicon-user"></span> Register';
  			$html .= '<li class="menu-item"><a href="' . wp_registration_url() . '" class="menu-item-go-register">' . $menu_title_reg . '</a></li>';			
  		} else {
  			$menu_title_logout = '<span class="glyphicon glyphicon-log-out"></span> Logout';
  			$html .= '<li class="menu-item"><a href="' .  wp_logout_url() . '" class="menu-item-go-logout">' . $menu_title_logout . '</a></li>';			
  		}
    }

    return $items . $html;
}

add_filter( 'wp_nav_menu_items', 'modify_menu56484', 10, 2 );