Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have the following secondary navigation menu on my website which has been built using the native WordPress Appearance > Menus feature:

secondary navigation menu


I am also using the following lines of code in my functions.php file to remove and add the 'Login / Register' and 'Logout' hyperlinks depending on the access privileges of the user:

// Add a Login hyperlink to the secondary navigation menu if the user is logged-out
function wpa_remove_menu_item( $items, $menu, $args ) {
    if( is_admin() || ! is_user_logged_in() ) return $items;

    foreach ( $items as $key => $item ) {
        if ( 'Login / Register' == $item->title ) unset( $items[$key] );
    }

    return $items;
}

add_filter( 'wp_get_nav_menu_items', 'wpa_remove_menu_item', 10, 3 );


// Remove the Logout hyperlink from the secondary navigation menu when the user is logged-in
function wpa_add_menu_item( $items, $menu, $args ) {
    if( is_user_logged_in() ) return $items;

    foreach ( $items as $key => $item ) {
        if ( 'Logout' == $item->title ) unset( $items[$key] );
    }

    return $items;
}

add_filter( 'wp_get_nav_menu_items', 'wpa_add_menu_item', 10, 3 );

Which is output like so:

enter image description here enter image description here


The Logout menu item is using the following hyperlink:

enter image description here

How can I replace this URL so that when the user selects the Logout item in the secondary navigation menu, it does not redirect them to a confirmation screen...

enter image description here

and is still positioned within the same hierarchy (within the 'My Account' parent menu of the 'Secondary Header', not at the start or end of the entire menu)?

enter image description here

Thank you.

share|improve this question

2 Answers 2

up vote 1 down vote accepted

Not tested, but it might work, use wp_logout_url() wordpress in built function to get logout url.

// Add a Login hyperlink to the secondary navigation menu if the user is logged-out
function wpa_remove_menu_item( $items, $menu, $args ) {
    if( is_admin() || ! is_user_logged_in() ) return $items;

    foreach ( $items as $key => $item ) {
        if ( 'Login / Register' == $item->title ) unset( $items[$key] );
        if ( 'Logout' == $item->title ){$items[$key]->url=wp_logout_url();}
    }

    return $items;
}

add_filter( 'wp_get_nav_menu_items', 'wpa_remove_menu_item', 10, 3 );
share|improve this answer

You can log someone out without using the standard logout page via the wp_logout function.

Here's an example that will work if you're using pretty permalinks, which hooks the parse_query action, checks if the requested URL is logout, and if so logs the user out and redirects them to the home page-

function wpd_logout( $query ){
    if( isset( $query->query_vars['pagename'] )
    && $query->query_vars['pagename'] == 'logout' ):
        wp_logout();
        wp_redirect( home_url('/') );
        exit;
    endif;
}
add_action( 'parse_query', 'wpd_logout' );
share|improve this answer
    
Hi there Milo, could you please provide me with some assistance in implementing your suggestion? I have pasted this into the functions.php file and have activated the pretty permalinks setting. –  user1752759 Apr 25 at 6:01
    
it's the same as your current code, just change the URL of your logout menu item to /logout/ –  Milo Apr 25 at 6:04
    
Hi there Milo, just to clarify what I have - i.stack.imgur.com/johBZ.png and i.stack.imgur.com/9BZ9q.png. This redirects to a page that does not exist and keeps the user logged-in. –  user1752759 Apr 25 at 6:12
    
is your WordPress install in the root of your domain? –  Milo Apr 25 at 6:26
    
unfortunately, no. –  user1752759 Apr 25 at 6:27

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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