The logout function in WordPress uses nonces for security, so if you try and go to yoursite.com/wp-login.php?action=logout, you’ll arrive at a page asking you to confirm that you intended to logout.
That’s not very fluid, but luckily, there’s a way around it.
Using the wp_logout_url function in conjunction with wp_nav_menu_items, we can add a logout link to any menu without requiring the user to confirm that they want to logout, because the function adds the nonce to the link automatically.
All you need to do is change the ID of the menu (5 in this case) to that of the menu that you’re trying to target. If you know the theme_location of the menu instead of the ID, you can change it to:
You can also indicate which page you want the user redirected to after they logout. Change the $logoutlink to the following to change it: