0

I want, as an exception, do a redirect if a user reaches a point where he needs to be logged in, to the login screen, and as soon as he logs in and certian conditions are fullfilled (a flag set as myappname_allow_redirect) I want to redirect back to the last url the user was at

In some shortcode that requires the user to be logged in:

try{

  $mus = new myappnameUserService();
}catch (myappnameNeedsLoginException $e){
  global $wp;
 //this is the easiest way supposedly to get the current URL see here https://wordpress.stackexchange.com/questions/274569/how-to-get-url-of-current-page-displayed
  $thisUrl = home_url($wp->request);
  $thisUrl =  urlencode($thisUrl);
  myappname_redirect_to_url('/wp-login.php?redirect_to=' . $thisUrl . '&myappname_allow_redirect=1');
}

functions.php

    function myappname_login_redirect($redirect_to, $request, $user)
    {

      if ($user instanceof WP_User) {
        if (isset($user->roles) && is_array($user->roles)) {
          //check for admins
          if (in_array('administrator', $user->roles)) {
            // redirect them to the default place
            return $redirect_to;
          } else {
            //if something is set her for exceptions, we allow regular redirect as well
            if($_GET['myappname_allow_redirect']){
                return $redirect_to;
            }else{
//but normally we redirect to a standard url
                return 'somethingelse';
            }
          }
        } else {
          return $redirect_to;
        }
      }
      //is there a user to check?

    }

    add_filter('login_redirect', 'myappname_login_redirect', 10, 3);

first it seems to work, I get redirected to mydomain.com/wp-login.php?redirect_to=xxxx&myappname_allow_redirect=1

but

myappname_login_redirect($redirect_to, $request, $user)

$redirect_to is empty, and so is $request

  • This doesn't answer your question as to why the variables are empty, but I've found the best way to redirect is to hook into the template_redirect filter and use wp_redirect() function directly. – Peter HvD Jan 30 '18 at 11:12
  • @Peter the problem is I only want to trigger this after successful login – Toskan Jan 30 '18 at 21:46
0

the issue was...

myappname_login_redirect needs to return $redirect_to in any case

otherwise the redirect to gets lost

background:

wp-login.php will render the redirect_to value into an input hidden into the login form.

so arriving at

wp-login.php?redirect_to=www.example.com

will render

an

<input type="hidden" value="www.example.com">

this value will be empty, if the filter does not return it by default

so fix...

   function myappname_login_redirect($redirect_to, $request, $user)
    {

      if ($user instanceof WP_User) {
        if (isset($user->roles) && is_array($user->roles)) {
          //check for admins
          if (in_array('administrator', $user->roles)) {
            // redirect them to the default place
            return $redirect_to;
          } else {
            //if something is set her for exceptions, we allow regular redirect as well
            if($_GET['myappname_allow_redirect']){
                return $redirect_to;
            }else{
//but normally we redirect to a standard url
                return 'somethingelse';
            }
          }
        } else {
          return $redirect_to;
        }
      }
      //is there a user to check?

return $redirect_to;
    }

    add_filter('login_redirect', 'myappname_login_redirect', 10, 3);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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