1

Context: a custom plugin

My goal: i need to redirect user to another site after a logout

I wrote these

error_log("test of error logging");

add_action('wp_logout', function () {
  error_log("do_action wp_logout_called");
});

add_action('wp_logout', function()
  {
    wp_redirect('https://www.google.it');
    exit();
  };

In the error_log i can see test of error logging, so plugin is loaded and error logging works

But

  1. I never see do_action wp_logout_called, never logged at all
  2. The redirect is not working (the user returns to wordpress, no error, no redirect)

What am I doing wrong?

Edit 1 - More trial

I tried a manual logut using

http://www.domain.ext/wp-login.php?action=logout&redirect_to=https%3A%2F%2Fwww.google.it&_wpnonce=a7b9f2e2b0

Esit: logout works, but i'm redirected to home page.

3
  • The second add_action does not have a valid second parameter, I would expect that code to produce a PHP syntax error, have you modified it for the question to hide things?
    – Tom J Nowell
    Commented Oct 24 at 9:39
  • Thanks for pointing the error. Anyway was only a copy/paste error.
    – realtebo
    Commented Oct 25 at 10:03
  • If I run the above code directly in a plugin file, it seems to error log which leads me to believe there's a 3rd party code preventing it. I tested this through admin page log out, not any specific log out form/button. I'd try to isolate the code, maybe it has to do when/how the action is being added. Disabling themes and plugins to help narrow down the issue as well.
    – Howdy_McGee
    Commented Oct 25 at 15:33

1 Answer 1

0

I also tested the code on a local sandbox WP and it worked as expected. As Howdy_McGee commented this is probably a conflict with 3rd party code.

If disabling theme and plugins is not an option, you can try changing the priority of the actions - i.e. the third parameter to add_action().

By default priority is 10, but if you give it a lower number it will be executed earlier - possibly before 3rd party actions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.