0

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!

5
  • 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
    Commented Feb 12, 2013 at 16:20
  • That's what I understood with the headers error. Is there anything you can recommend?
    – Ina
    Commented Feb 12, 2013 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
    Commented Feb 12, 2013 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
    Commented Feb 12, 2013 at 17:55
  • Thank you for taking the time answer. I really appreciate it.
    – Ina
    Commented Feb 12, 2013 at 17:55

1 Answer 1

0

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.

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.