Support » Developing with WordPress » LogOut not working anymore

  • Hi,

    I accidently deleted the entire menu instead of just one menu item… Now I tried to set it up again but I experience an issue with the LogOut.

    This used to work like a charm before I deleted the menu:

    add_action('wp_logout','auto_redirect_after_logout');
    function auto_redirect_after_logout(){
      wp_safe_redirect( home_url() );
      exit();
    }

    Now, after I recreated the menu, clicking on “LogOut” doesn’t do anything. The user remains still logged in. What can I do to fix this? :-/ The user should be directed to the homepage after clicking on “LogOut”.

    Thanks

Viewing 4 replies - 1 through 4 (of 4 total)
  • Hi @veshop

    You can follow this guide https://pluginsforwp.com/blog/wordpress-logout-link/

    It shows you 2 methods, one is to simply add a link like https://yourwebsite.com/wp-login.php?action=logout which triggers a prompt and requires another click to logout. Simple & effective. But if you want it to work in a single click (As soon as its clicked), there is a piece of code you need to use which is provided later in the article.

    Thread Starter veshop

    (@veshop)

    Hi @ashfame

    no, not working. It crashed my website. When I just use the url for logout, I see a logout in the menu but regarding the page content I still see everything… And when I insert the code snippet my site is crashed. When I try to login, I get immediately a 404. Any other idea?

    Thread Starter veshop

    (@veshop)

    oK, I found something that does works 🙂

    Maybe it helps somebody else too:

    1. Create an ‘LogOut’ custom menu item with the link

    • ‘/wp-login.php?action=logout’
    • 2. Add first this to your function.php of your child-theme to initiate the logout:

      function redirect_after_logout(){
          wp_redirect( home_url() );
          exit();
      }
      add_action('wp_logout', 'redirect_after_logout');

      3. To remove the ‘Are you sure you want to logout?’ then also add this to your function.php of your child-theme:

      add_action('check_admin_referer', 'logout_without_confirm', 10, 2);
      function logout_without_confirm($action, $result)
      {
          /**
           * Allow logout without confirmation
           */
          if ($action == "log-out" && !isset($_GET['_wpnonce'])) {
              $redirect_to = isset($_REQUEST['redirect_to']) ? $_REQUEST['redirect_to'] : 'url-you-want-to-redirect';
              $location = str_replace('&', '&', wp_logout_url($redirect_to));
              header("Location: $location");
              die;
          }
      }

    1 and 3 were the things mentioned on the link I gave. But glad its working as per your expectation now.

Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.