Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

E.g.

<?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.

Thanks!

share|improve this question
1  
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
add comment

1 Answer

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
add comment

Your Answer

 
discard

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.