2

I have a sign out on my site to log out of Wordpress

When logged out I would like to redirect uses to a different URL.

I'm using this in the functions.php

 add_action(' wp_logout ',' auto_redirect_external_after_logout '); function auto_redirect_external_after_logout(){ wp_redirect( ' http://redirect-url ' ); exit(); } 

and this in the header

 <li class="signOut"><?php wp_logout(); ?></li> 

When I run this I get a long list of errors in the page

 Warning: Cannot modify header information - headers already sent by 

2 Answers 2

9
<li class="signOut"><?php wp_logout(); ?></li> 

That is the offending code, you are calling the wp_logout function that will log the user out and to do that WordPress needs to send the info (headers) to the browser and hence the error.

So the final action code should look like

add_action( 'wp_logout', 'auto_redirect_external_after_logout'); function auto_redirect_external_after_logout(){ wp_redirect( 'http://redirect-url' ); exit(); } 

and the logout link should be changed to

<li class="signOut"><a href="<?php echo wp_logout_url(); ?>" title="Logout">Logout</a></li> 
Sign up to request clarification or add additional context in comments.

1 Comment

All you've got to change is the logout link and your action hook will work fine.
2

If you want to use that hook, you're going to need to use JavaScript, since headers have already been sent:

add_action(' wp_logout ',' auto_redirect_external_after_logout '); function auto_redirect_external_after_logout(){ echo '<script>window.location.href = "http://redirect-url"</script>'; exit(); } 

Alternatively, a more elegant way is to use the wp_logout_url() function in place of your current logout link, and scrap the hook all together. Usage:

<a href="<?php echo wp_logout_url( 'http://redirect-url' ); ?>" title="Logout">Logout</a> 

1 Comment

mevius - your first javascript solution still give me the Warning: Cannot modify header information. The second solution logs me out put takes me to the wordpress login page and not the url stated

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.