简体   繁体   中英

Redirecting user to url with username wordpress

I want to redirect users after login based on username.

add_filter( 'login_redirect', 'wppbc_custom_login_redirect', 99, 3 );
function wppbc_custom_login_redirect( $redirect_to, $requested_redirect_to, 
$user ) {
   if( is_wp_error( $user ) ) {
      return $redirect_to;
   }
   $username = $user->user_login;
   if ( is_array( $form_args ) ) {
      switch ( $username ) {
     case 'User1':
        $redirect_to = 'https://www.example.com/User1';
        break;
     case 'User2':
        $redirect_to = 'https://www.example.com/User2';
        break;
     default:
        $redirect_to = 'https://www.example.com';
      }
   }
   return $redirect_to;
}

The only problem is that no i have to manually add every user to this list with the domain i want them to be redirected to after a succesfull login. I want a single code where i can add the Username variable to the domain.

User name x logs in succesfully and will be redirected to www.example.com/x User name y logs in succesfully and will be redirected to www.example.com/y

Can anyone help me with this code?

You can not do like this have to post back the javascript variable to your server before the server can handle the value. To do this you can either program a javascript function that submits a form - or you can use ajax / jquery. jQuery.post

Maybe the most easiest approach for you is something like this

function myJavascriptFunction() { 
  var javascriptVariable = "John";
  window.location.href = "myphpfile.php?name=" + javascriptVariable; 
}

On your myphpfile.php you can use $_GET['name'] after your javascript was executed.

Ok i don't understand a clue of javascript haha. The redirect

$redirect_to = 'https://www.example.com/' . $username;

Doesn't work..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM