WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm looking for the simplest way to redirect a password protected page, to another page after entering the password.


<?php if ( is_page('name-of-page') && ! post_password_required() ) { 
   wp_redirect( home_url() ); exit; 
} ?>

Using wp_redirect is triggering a 'headers already sent error'.

I've seen some solutions using output buffering to avoid this but, I'm not sure if it is the best solution.


share|improve this question
You can't call wp_redirect in the middle of a template, it needs to be done before anything else is printed out – Tom J Nowell Feb 12 '13 at 16:20
That's what I understood with the headers error. Is there anything you can recommend? – Ina Feb 12 '13 at 17:41
The only non-hackish guaranteed solution is to do it earlier before the page loads. btw how is a user to read the password protected page? It sounds like you're trying to fix a fix, like the "I know an old lady who swallowed a fly" song? Are you trying to implement a membership area? – Tom J Nowell Feb 12 '13 at 17:43
I thought so. No, I'm not trying to implement a membership area. It's just one page, and it's not ideal but it was a request. – Ina Feb 12 '13 at 17:55
Thank you for taking the time answer. I really appreciate it. – Ina Feb 12 '13 at 17:55
up vote 0 down vote accepted

Tom J Nowell confirmed what was happening. Inserting wp_redirect in a template causes the page either to partially load or not load at all.

It needed to be placed before the opening <html> to work as it would load everything before </head> then stop.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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