WordPress.org

Ready to get started?Download WordPress

Forums

Password protected posts redirecting users to wp-admin when password is correct (3 posts)

  1. David Gard
    Member
    Posted 1 year ago #

    Hey all,

    I set up some posts on my site with password protection, but WP was redirecting me to /wp-admin/ everytime a password was input. I found out why this was happening, so I thought I'd share my solution, as I cannot be the only one with this issue.

    This occured using the WP default password form, having no plugins active, and with the Twenty Eleven theme (as well as with my own custom theme & passwrod form, and with some plugins active).

    Upon investigation I found that the form is posted to wp-login.php?action=postpass, and in all cases under the relevent section of code, wp_get_referrer() was returning the correct link as the referrer. This seemed to point to wp_safe_redirect($location) as the culprit, so I checked the $location variable at various stages during that function execution and found that wp_validate_redirect($location, admin_url()) was returning /wp-admin/ as the location.

    Upon further reading of the wp_validate_redirect() function, I found that this is becuase my 'wp_url' and 'site_url' settings are different, so the redirect was failing the validation.

    The solution here turned out to be very simple - add my host to a list of 'allowed hosts'. Place this code in your functions.php file and you should be good to go -

    add_filter('allowed_redirect_hosts', 'amend_redirect_hosts', 10, 2);
    function amend_redirect_hosts($allowed_hosts, $this_host){
    
    	$allowed_hosts[] = $this_host;
    
    	return $allowed_hosts;
    
    }
  2. s_ha_dum
    Member
    Posted 1 year ago #

    That is kind-of a weird quirk though. Have you searched the Trac for this issue?

  3. esmi
    Forum Moderator
    Posted 1 year ago #

    I found that this is becuase my 'wp_url' and 'site_url' settings are different

    Can I ask what the differences were between the two urls? Was this a sub-folder install running at root?

Topic Closed

This topic has been closed to new replies.

About this Topic