Jan 26

WordPress redirects to login instead of post after commenting

Category: WWW   — Published by goeszen on January 26, 2017 at 2:20 pm

This WordPress blog here ran into an issue recently which is related to the Subdomains Plugin. As you might have noticed, categories on this blog are branched out onto dedicated subdomains. And this is done via the a WP Plugin called Subdomains.

After the last WP update, commenting suddenly stopped working (actually it might have been broken for a while now). Whenever someone submitted a comment to an article here on goesZen, WordPress would do its checks and computing - stuff like writing the comment to the database etc. - and after that the reader is redirected to the originating post.

WP does so by figuring out where to redirect the user and putting the location into a variable. This variable is then filtered through wp_safe_redirect() which is to make sure WP isn't redirecting the commenter to some offsite/ not-allowed host.

This is where the conflict with Subdomains happens. For WordPress a subdomain like linux.goeszen.com is an non-whitelisted host and refuses to redirect there, sending the user either into an endless redirection loop (if something else goes awry), or to the admin log-in page (which is the wanted behaviour, but just as wrong in our case).

Believe it or not, it seems other users have the same issue.

The fix is to add a filter routine to your functions.php file, as outlined here:

add_filter( 'allowed_redirect_hosts' , 'my_allowed_redirect_hosts' , 10 );
function my_allowed_redirect_hosts($content){
$allowed = array(
'subdomain1.example.com',
'subdomain2.example.com',
'subdomain3.example.com'
);
return $allowed;
}

The above is a hard-coded solution. With a few lines more, it could be expanded into a more dynamic variant.

But there's a big caveat: when you apply this filter, logging into the admin backend won't work. (You'll be stuck in an endless "being redirected back to login page" loop) So either commenting will work, or logging into the backend...

Not an ideal solution, but a hotfix until the author patches the Subdomains Plugin.

Search engine keywords:
error wordpress on comment redirected to log in
wordpress comment redirected to log in
wordpress redirects to login instead of post on comment
commenting on a subdomain post results in user being redirect to admin login