I have the following secondary navigation menu on my website which has been built using the native WordPress Appearance > Menus feature:
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:
The Logout menu item is using the following hyperlink:
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...
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)?
Thank you.