Add a logout link without going to confirmation page

Padlocks

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:

9 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 ) {
    if(is_user_logged_in()){
    $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: https://gist.github.com/theukedge/24a3d3ec8b61197a094e

  4. Recyclart says:

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

  5. Craig says:

    Hello,

    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!

    Craig.

    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) {

    ob_start();
    wp_loginout(‘index.php’);
    $loginoutlink = ob_get_contents();
    ob_end_clean();

    $items .= ”. $loginoutlink .”;

    return $items;
    }

Leave a Reply