Redirect after login, Wordpress URLs

2019-09-06 23:49发布

问题:

I'm using the following function to redirect a user after a failed login attempt. In functions.php, I added:

add_action( 'wp_login_failed', 'inline_login_fail' );

function inline_login_fail( $username ) {
    $referrer = $_SERVER['HTTP_REFERER'];
    if ( !empty($referrer) && !strstr($referrer,'wp-login') && !strstr($referrer,'wp-admin') ) {
        if ( !strstr($referrer,'?login=failed') ) { // don’t append twice
        wp_redirect( $referrer . '?login=failed' ); 
        } else {
        wp_redirect( $referrer );
        }
    exit;
    }
}

This works, but the problem is I can't figure out how to parse the '?login-failed'. When I asked this as a PHP question, they said the URL is malformed.

My URL: www.site.com/?page_id=57?login=failed

Preferred URL: www.site.com/?page_id=57&login=failed

Now you would think I could just change the '?' to '&'. When I do this, it creates an issue if the user is trying to login from the home page and fails. It creates a URL which is a 404: "The requested URL /wordpress/&login=failed was not found on this server."

Is there any way to correct this function so it will work with the preferred URL? That's so I can retrieve the value of whether login has failed, like this:

<?php
if(isset($_GET['login']) === true and $_GET['login'] === 'failed')
{
  get_template_part( 'login', 'failed' );
} else {
  get_template_part( 'login', 'form' );
}
?>

Otherwise, I can't figure out how to work with that URL at all. Thanks!

回答1:

check for ?

<?php
    function inline_login_fail( $username ) {
        $referrer = $_SERVER['HTTP_REFERER'];
        if ( !empty($referrer) && !strstr($referrer,'wp-login') && !strstr($referrer,'wp-admin') ) {
            if ( !strstr($referrer,'login=failed') ) { // don’t append twice
                if(!strstr($referrer, '?')){
                    wp_redirect( $referrer . '?login=failed' ); 
                } else {
                    wp_redirect( $referrer . '&login=failed' ); 
                }
            } else {
                wp_redirect( $referrer );
            }
        exit;
        }
    }
?>


标签: php wordpress