Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I've been using Gravity Forms to log users in directly after they register on our site. Basically, the user registers with a User Registration form and then they are directed to a gated 'Thank You' page with a free download. The download page is gated and basically redirects the user if they are not logged in. The Gravity Forms registration form doesn't automatically log the user in after they register so when they are forward to the 'Thank You' page, it automatically redirects them away.

So what I've done is set up a GET variable login so the user registers, is redirected to login with the get variables automatically and then is redirected to the 'Thank You' page after they are logged in:

login to wordpress with Get variables instead of Post

This works fine except now I have to do it for two other languages (Chinese and Japanese). These login pages have their own URLs (wp-login_cn.php and wp-login_jp.php). So what I've done is tried to write some more conditional statements in the PHP theme functions to make this work but it always redirects back to the English Thank You page. Here's my code below (it's basically the code from the above link with some extra changes):

//LOGIN FUNCTION FOR REGISTER PAGE ON SITE
add_action('init', 'GET_login');
function GET_login() {
    //Check that we are on the log-in page
    if(in_array($GLOBALS['pagenow'], array('wp-login.php'))):

    //Check that log and pwd are set
        if(isset($_GET['log']) && isset($_GET['pwd'])):
            $creds = array();
            $creds['user_login'] = $_GET['log'];
            $creds['user_password'] = $_GET['pwd'];
            $creds['remember'] = true; //Do you want the log-in details to be remembered?

            //Where do we go after log-in?
            $redirect_to = admin_url('profile.php');

            //Try logging in
            $user = wp_signon( $creds, false );

            if ( is_wp_error($user) ){
                //Log-in failed
            }else{
                //Logged in, now redirect
                $redirect_to = admin_url('/download/');
                wp_safe_redirect($redirect_to);
            exit();
            }
        endif;
    endif;
    //If we are not on the log-in page or credentials are not set, carry on as normal
}

add_action('init', 'GET_login_cn');
function GET_login_cn() {
//Check that we are on the Chinese log-in page
    if(in_array($GLOBALS['pagenow'], array('wp-login_cn.php'))):

    //Check that log and pwd are set
        if(isset($_GET['log_cn']) && isset($_GET['pwd_cn'])):
            $creds = array();
            $creds['user_login'] = $_GET['log_cn'];
            $creds['user_password'] = $_GET['pwd_cn'];
            $creds['remember'] = true; //Do you want the log-in details to be remembered?

            //Where do we go after log-in?
            $redirect_to = admin_url('profile.php');

            //Try logging in
            $user = wp_signon( $creds, false );

            if ( is_wp_error($user) ){
                //Log-in failed
            }else{
                //Logged in, now redirect
                $redirect_to = admin_url('/zh-hans/download/');
                wp_safe_redirect($redirect_to);
            exit();
            }
        endif;
    endif;
    //If we are not on the log-in page or credentials are not set, carry on as normal
}

add_action('init', 'GET_login_jp');
function GET_login_jp() {
//Check that we are on the Japanese log-in page
    if(in_array($GLOBALS['pagenow'], array('wp-login_jp.php'))):

    //Check that log and pwd are set
        if(isset($_GET['log_jp']) && isset($_GET['pwd_jp'])):
            $creds = array();
            $creds['user_login'] = $_GET['log_jp'];
            $creds['user_password'] = $_GET['pwd_jp'];
            $creds['remember'] = true; //Do you want the log-in details to be remembered?

            //Where do we go after log-in?
            $redirect_to = admin_url('profile.php');

            //Try logging in
            $user = wp_signon( $creds, false );

            if ( is_wp_error($user) ){
                //Log-in failed
            }else{
                //Logged in, now redirect
                $redirect_to = admin_url('/ja/download/');
                wp_safe_redirect($redirect_to);
            exit();
            }
        endif;
    endif;
    //If we are not on the log-in page or credentials are not set, carry on as normal
}

With that I made all different functions. I've also tried it as all the same if statement, see below:

//LOGIN FUNCTION FOR REGISTER PAGE ON SITE
add_action('init', 'GET_login');
function GET_login() {
    //Check that we are on the log-in page
    if(in_array($GLOBALS['pagenow'], array('wp-login.php'))):

//Check that log and pwd are set
    if(isset($_GET['log']) && isset($_GET['pwd'])):
        $creds = array();
        $creds['user_login'] = $_GET['log'];
        $creds['user_password'] = $_GET['pwd'];
        $creds['remember'] = true; //Do you want the log-in details to be remembered?

        //Where do we go after log-in?
        $redirect_to = admin_url('profile.php');

        //Try logging in
        $user = wp_signon( $creds, false );

        if ( is_wp_error($user) ){
            //Log-in failed
        }else{
            //Logged in, now redirect
            $redirect_to = admin_url('/download/');
            wp_safe_redirect($redirect_to);
        exit();
        }
    endif;
endif;
//If we are not on the log-in page or credentials are not set, carry on as normal

//IF WE ARE ON THE CHINESE LOGIN PAGE
if(in_array($GLOBALS['pagenow'], array('wp-login_cn.php'))):

//Check that log and pwd are set
    if(isset($_GET['log_cn']) && isset($_GET['pwd_cn'])):
        $creds = array();
        $creds['user_login'] = $_GET['log_cn'];
        $creds['user_password'] = $_GET['pwd_cn'];
        $creds['remember'] = true; //Do you want the log-in details to be remembered?

        //Where do we go after log-in?
        $redirect_to = admin_url('profile.php');

        //Try logging in
        $user = wp_signon( $creds, false );

        if ( is_wp_error($user) ){
            //Log-in failed
        }else{
            //Logged in, now redirect
            $redirect_to = admin_url('/zh-hans/download/');
            wp_safe_redirect($redirect_to);
        exit();
        }
    endif;
endif;

//IF WE ARE ON THE JAPANESE LOGIN PAGE
if(in_array($GLOBALS['pagenow'], array('wp-login_jp.php'))):

//Check that log and pwd are set
    if(isset($_GET['log_jp']) && isset($_GET['pwd_jp'])):
        $creds = array();
        $creds['user_login'] = $_GET['log_jp'];
        $creds['user_password'] = $_GET['pwd_jp'];
        $creds['remember'] = true; //Do you want the log-in details to be remembered?

        //Where do we go after log-in?
        $redirect_to = admin_url('profile.php');

        //Try logging in
        $user = wp_signon( $creds, false );

        if ( is_wp_error($user) ){
            //Log-in failed
        }else{
            //Logged in, now redirect
            $redirect_to = admin_url('/ja/download/');
            wp_safe_redirect($redirect_to);
        exit();
        }
    endif;
endif;
//If we are not on the log-in page or credentials are not set, carry on as normal
}

I even set the GET variables to different values so I would ensure that it wouldn't run if it accidentally was sent through the script on the US login page. You'll notice the redirect to goes to three different URLs that are all different language pages of the same page.

I've been trying to find where the issue lies and I'm curious if it's in my syntax above. I was almost positive that this should work but I may be misunderstanding how the functions run in WordPress.

Thanks for your help!

share|improve this question
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.