0

I am currently trying to change Woocommerce redirect url for login only when users logged from a specific page. So first I tried that way in my template page:

<?php woocommerce_login_form(array('redirect'=>$_SERVER['REQUEST_URI'])); ?>

Then, I saw user is still redirected to default (my-account) so I used the login hook that way:

function share_login_redirect( $redirect ) {
    if( strtr($_SERVER['REQUEST_URI'],array('/partage','/share')) ){
        $redirect = get_site_url().$_SERVER['REQUEST_URI'];
    }
    return $redirect;
}
add_filter( 'woocommerce_login_redirect', 'share_login_redirect', 10, 2 );

All of thoses techniques didn't work for me. Knowing that $redirect = "https://mysite/share/?param=xx" what am I doing wrong there?

2017-08-29 EDIT : If anyone had the same problem as I did, it is possible a plugin uses the same hook than you and have an higher priority than yours meaning the redirect will not work until you move up your priority. Please see my answer below.

closed as off-topic by Mark Kaplun, Jack Johansson, cjbj, Max Yudin, Howdy_McGee Sep 12 '17 at 15:31

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

2

Try changing strtr to strpos. However, you can not give it an array for needles I don't think. You will have to create another function for that.

function strposa($haystack, $needles=array(), $offset=0) {
        $chr = array();
        foreach($needles as $needle) {
                $res = strpos($haystack, $needle, $offset);
                if ($res !== false) $chr[$needle] = $res;
        }
        if(empty($chr)) return false;
        return min($chr);
}

Use this function instead of strtr

  • Hi, thanks for your suggestion. However, my condition worked well without any changes. I just think "woocommerce_login_redirect" hook do not work as intendend, maybe something interfere with it. I will need to investigate more. – Dynomite Aug 29 '17 at 17:17
  • 1
    This: strtr($_SERVER['REQUEST_URI'],array('/partage','/share') will always return a string and therefor always be true. Here is the function definition: http://php.net/manual/en/function.strtr.php. The only time that strtr will return false is when the key is "" empty. The array you have provided is not associative also as it should be in the function definition above. But the keys are auto assigned as 0,1,2 etc. So it will not be empty. Therefore you will never get a false. – Jeff Mattson Aug 29 '17 at 17:34
  • well I must say that you are right. This was another problem without even knowing, thanks for the clarifications. That being said, my main problem was of another nature and has been fixed. – Dynomite Aug 29 '17 at 19:29
2

I found what was my problem is priority. Some other plugin was using a 1000 priority, so I had to boost my priority when declaring the hook. That way:

add_filter( 'woocommerce_login_redirect', 'share_login_redirect', 1100, 2 );

instead of

add_filter( 'woocommerce_login_redirect', 'share_login_redirect', 10, 2 );

Not the answer you're looking for? Browse other questions tagged or ask your own question.