简体   繁体   English

使用用户名wordpress将用户重定向到url

[英]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. 我想要一个可以在其中将Username变量添加到域的代码。

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 用户名x成功登录,并将重定向到www.example.com/x用户名y成功登录,并将重定向到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. 您不能这样做,必须将javascript变量发回服务器,然后服务器才能处理该值。 To do this you can either program a javascript function that submits a form - or you can use ajax / jquery. 为此,您可以编写提交表单的JavaScript函数,也可以使用ajax / jquery。 jQuery.post 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. 在执行JavaScript后,您可以在myphpfile.php上使用$ _GET ['name']。

Ok i don't understand a clue of javascript haha. 好的,我不了解JavaScript的提示。 The redirect 重定向

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

Doesn't work.. 不起作用..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Safari 中的用户名和密码重定向到 FTP URL - Redirecting to FTP URL with username and password in Safari 重定向到同一路由/ user / {{username}} Laravel 4+ - Redirecting to the same route /user/{{username}} Laravel 4+ WordPress的URL重写以添加用户名 - wordpress url rewrite to add username 如何在 Wordpress 中将用户名添加到 url? - How to add username to url in Wordpress? 在没有用户名的wordpress中添加用户 - Add User in wordpress without username 将登录的Wordpress用户重定向到登录屏幕之外,并根据其自定义元数据返回特定的URL - Redirecting a logged-in wordpress user away from the sign-in screen and back to a specific url based on their custom metadata WordPress:如何在Wordpress中按用户名搜索用户 - Wordpress : how to search a user by username in wordpress HTACCESS将URL / $ USERNAME重写为/ user / profile / USERNAME - HTACCESS rewrite url /$USERNAME to /user/profile/USERNAME 创建一个重定向,将登录用户的用户名添加到 WordPress 中的网站 url - Create a Redirect which adds logged in user's username to website url in WordPress 无法在Wordpress中重定向首页网址,但无法重定向目录网址 - Trouble redirecting homepage url in wordpress but keep directory urls from redirecting
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM