February 12, 2020
I’ve seen a lot of suggestions online to copy the Logout link shown in the WordPress backend into a Custom Link when using wp_nav_menu().
However, this will redirect users to the Logout Confirmation screen, and they will be prompted to logout once more.
Use the below snippet in functions.php to target a Menu Item by title and slug to create a logout link with nonce:
function change_menu($items){
foreach($items as $item){
if( $item->title == "Logout"){ // Update to link title
$item->url = $item->url . "&_wpnonce=" . wp_create_nonce( 'log-out' ); // do not change
}
}
return $items;
}
add_filter('wp_nav_menu_objects', 'change_menu');