WordPress password protected page: white screen after login

If you’ve encountered a white screen after entering a password on a protected WordPress post, you’re not alone. This issue can be quite frustrating as there’s very little indication as to what is wrong.

What Causes the White Screen Issue?

The white screen issue in WordPress, also referred to as the “White Screen of Death,” can occur for many reasons. But in the context of password-protected, it’s usually due to a problem with the way WordPress handles form submissions and redirects. When you submit a password to view protected content, WordPress typically redirect them back to the same page. However, if the process isn’t handled correctly or there is a misconfiguration on the website, this results in WordPress not knowing where to redirect the user after the password is submitted. E.g. your website is example.com but the form is submitted from www.example.com.

To solve this problem, we can modify the password form to include a hidden ‘_wp_http_referer’ field which will tell WordPress where to redirect the user after they enter the password thus preventing the white screen from occurring.

Here’s a simple code snippet that you can add to your theme’s ‘functions.php’ file:

<?php
function add_custom_referer_to_password_form( $output ) {
    $referer_input = sprintf(
        '<input type="hidden" name="_wp_http_referer" value="%s">',
        esc_url( get_permalink() )
    );
    return str_replace( '</form>', $referer_input . '</form>', $output );
}
add_filter( 'the_password_form', 'add_custom_referer_to_password_form' );

How does this work?

First we hook into the form using the add_filter function which allows us to modify the HTML of the form. We’re using the ‘the_password_form’ filter which allows us to alter the form before it’s displayed on the page.

Then we add a hidden input field that contains the URL of the current post with the ‘_wp_http_referer’ name. Now we return the modified form with the new input field included so that WordPress knows where to redirect the user after submitting the password.

As you can see while the issue seems to be a bit of a headache, the fix is relatively simple. If you’re not comfortable making this type of change, consider reaching out to a developer for assistance with this quick fix.



1 Comment

  • Lindsey John

    Nice fix for the WordPress password-protected page white screen issue! The code snippet you shared for adding the referer field is super helpful. Thanks for making it easy to follow!

Leave a Reply