0

I've written this login/logout for my functions.php in my wordpress theme. The problem is, it's showing a login/logout link in all menus both primary and secondary. Is there a way to get it to appear only in secondary?

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

This is what I ended up using:

add_filter('wp_nav_menu_items', 'add_login_logout_link', 10, 2);
function add_login_logout_link($items, $args) {
if( $args->theme_location == 'top-menu' ) {
    ob_start();
    wp_loginout('index.php');
    $loginoutlink = ob_get_contents();
    ob_end_clean();
    $items .= '<li>'. $loginoutlink .'</li>';
}
return $items;
}
1

Change the filter hook to wp_nav_menu_{menu-name}_items, so it will only run on a specific menu. For example, if the menu name is "Secondary", use the hook wp_nav_menu_secondary_items, or if the menu name is "Top Navigation", use wp_nav_menu_top-navigation_items (replace spaces in the menu name with dashes).

Your updated code, assuming the menu name is "Secondary" (the menu name, not the theme location), would be:

add_filter('wp_nav_menu_secondary_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;
}
0

As Nilamber Suggessts:

/**
 * Add Login/Logout to Menu
 */
function add_login_logout_link($items, $args) {
    if( $args->theme_location == 'primary' ) {
        $loginoutlink = wp_loginout('index.php', false);
        $items .= '<li>'. $loginoutlink .'</li>';
    }

    return $items;
}
add_filter('wp_nav_menu_items', 'add_login_logout_link', 10, 2);

Also note, using this function, theme_location cannot be null. The arg check negates the need to change the menu name in the filter.

0

As suggested by Nilamber, You will have to check the parameters defined in $args. The following code will work but you can modify it as your ease.

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

function add_loginout_link( $items, $args ) {
if (is_user_logged_in() && $args->theme_location == 'primary') {
    $items .= '<li><a href="'. wp_logout_url() .'">Log Out</a></li>';
}
elseif (!is_user_logged_in() && $args->theme_location == 'primary') {
    $items .= '<li><a href="'. site_url('wp-login.php') .'">Log In</a></li>';
}
return $items;
}
  • 1
    All these methods have been valid approaches, but can you explain your choice not to use wp_loginout(); – eyoung100 Sep 12 '14 at 4:02
  • @ECarterYoung Just a choice, any of them can be used, The main motive of answer was to clarify how to use $args in 'wp_nav_menu_items' filter. – WisdmLabs Sep 12 '14 at 5:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.