Add a logout link without going to confirmation page


The logout function in WordPress uses nonces for security, so if you try and go to, 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:

11 thoughts on “Add a logout link without going to confirmation page”

  1. Alex says:

    Thanks Dave, just what I was looking for.

  2. bronte says:

    I like this solution. I’m a noobie…where does this php code go? In an existing theme php file? In the functions.php file?

    1. This code, along with most other snippets should go in your theme’s functions.php file if you don’t have a functionality plugin on your site.

  3. Recyclart says:

    Thanks Dave for this tip.
    I would like to show the button only if the user is logged in otherwise the logout button is always visible.
    I’ve modified the code in this way:

    function diww_menu_logout_link( $nav, $args ) {
    $logoutlink = ‘ Logout‘;
    if( $args->theme_location == ‘main_menu’ ) {
    return $nav.$logoutlink ;
    } else {
    return $nav;

    add_filter(‘wp_nav_menu_items’,’diww_menu_logout_link’, 10, 2);

    But don’t seem to work, any idea, help ?
    Thanks !

    1. That’s because you’ve got two nested conditionals and you only closed one out, not both, so the else statement for is_user_logged_in is never used. Try this instead:

  4. Recyclart says:

    Thanks Dave for your help, it works like a charm ! :)

  5. Craig says:


    This is just what I’m after as well, however I can’t get it to work either in functions.php or in a custom plugin as per your link above and I’m not sure why. I’ve identified the menu id (term_id) from the wp_terms table, but it just won’t show the logout menu item. Any ideas what’s happening?

    Thanks in advance!


    1. Hi Craig,

      Without being able to see your code, I can’t think of a reason why this wouldn’t work. If it’s been correctly installed in an active theme/plugin, it should work.

  6. Craig says:

    Thanks for the reply. I found a similar snippet of code that worked in your DIY plugin on my site, but thanks anyway.

    add_filter(‘wp_nav_menu_items’, ‘add_login_logout_link’, 10, 2);
    function add_login_logout_link($items, $args) {

    $loginoutlink = ob_get_contents();

    $items .= ”. $loginoutlink .”;

    return $items;

  7. piyush says:

    Hi Dave,

    I used your code to generate logout link.After logout in particular situation it display confirmation page.
    Situation is:-
    1)Log in as user1 in tab1.
    2)open another tab and logout from that tab2.
    3)In tab2 log in as user2.
    4)now go to tab1 and try to logout from that tab,it will display confirmation page.

    I don’t want to see user can see the confirmation that situation what can i do ??


    1. Not much. It’s a function of the unusual manner in which you’re logging in and out

Leave a Reply