I am pretty new to Wordpress, so please forgive me if this sounds like a silly question.

I have a Wordpress Page which is using the default page.php template in my theme. I modified the page.php file to include an if-statement which calls code using get_template_part(); depending on whether the user is logged in or not.

If the user is logged in, I want the person to fill out the form, which has an action that is pointing to a Thank You page that I created. That page saves the contents of the form to the database and then redirects the user to a third party site. I need to log the user out of the Wordpress site before redirecting them, but for some reason the user stays logged in even after I use wp_logout(); and redirect using wp_redirect();

Here is example of the code I just described:

if ( is_user_logged_in() ) {
  get_template_part( 'page-templates/user-logged-in-page', 'user-is-logged-in' );
} else {
  get_template_part( 'page-templates/user-should-login-page', 'user-needs-to-login' );

Here is the code for the logic on the Thank You page that redirects to the third party site:

if ( isset( $_POST["submit_form"] ) && !empty($_POST["user_entry"]) ) {

  $user_entry = strip_tags($_POST["user_entry"], "");
  $user_id = get_current_user_id();
  update_user_meta( $user_id , 'user_entry' , $user_entry );

  wp_logout();  // This line is not working!

  wp_redirect( 'https://www.mythirdpartysite.com/' );


However, after the redirect happens, when I visit the Wordpress site again, I am still logged in!

Does anyone know why the wp_logout() function is not working for me?


Additionally, I am not sure if this has anything to do with the fact that the wp_logout function is not working as expected, but, the following is also a part of my code:

function app_output_buffer() {
add_action('init', 'app_output_buffer');
  • 1
    Where are these code snippets located? And at what point do they run? For these things the timing is important, you can't redirect and logout halfway down a page if you've already sent content for example – Tom J Nowell Aug 29 '18 at 18:59
  • This is all happening within a plugin. The code for the form itself is in the theme. I am having a hard time understanding how to do the redirect anywhere else. – Moose Aug 29 '18 at 20:28
  • wp_logout() and wp_redirect() with the ob_start() stuff, all worked well for me. So if you could show the whole code with the wp_logout() call there (including where it's being called - e.g. during the init, template_redirect, etc. "action"), maybe we could help you further. But I'm guessing that in your case, the cookies were failed to be set - maybe there has been an output sent to the browser. – Sally CJ Aug 30 '18 at 6:19
  • what is this whole "output already sent to browser" business. This is the error I am seeing onscreen (repeating about 20 times on the screen). – Moose Aug 30 '18 at 15:16
  • Well, it's exactly as it says.. you probably call the wp_logout() after there has been some output/content sent to the browser or printed on screen, even just a space. See the PHP manual for header() which wp_redirect() uses, and setcookie() which wp_logout() uses. Both the manual warn you about making sure no output has been sent to the browser prior to calling header() or setcookie(). – Sally CJ Aug 31 '18 at 1:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.