简体   繁体   English

Wordpress 重定向用户角色

[英]Wordpress Redirect for User Role

I'm using Wordpress with a User Role that should be redirected to /userrole when they try to visit / .我正在使用带有用户角色的 Wordpress,当他们尝试访问/时应该重定向到/userrole Can anyone help me out?谁能帮我吗? I tried a few things but it doesn't work.我尝试了几件事,但没有用。

This is what I have tried so far:这是我迄今为止尝试过的:

function redirect_users_by_role() { 
   $current_user = wp_get_current_user(); 
   $role_name = $current_user->roles[0];
   if('specuserrole' === $role_name) { 
      wp_redirect('website.com/userole'; ); } 
   } 
   add_action( '/'', 'redirect_users_by_role' );

It seems that you are hooking into a WordPress action of / which doesn't exist.您似乎正在使用不存在的/的 WordPress 操作。 I'm not sure if you have read about what actions are available to you, but there is a full list here .我不知道你是否读过哪些行为是提供给你,但是有一个完整列表在这里

Based on this, it appears as though you want to hook into an action, check that we are on the front page and then redirect accordingly.基于此,您似乎想要挂钩一个动作,检查我们是否在首页上,然后相应地重定向。 With that in mind I would suggest the below:考虑到这一点,我建议如下:

function redirect_users_by_role() { 
   $current_user = wp_get_current_user(); 
   $role_name = $current_user->roles[0];
   if('specuserrole' === $role_name && is_page('home')) { 
      wp_redirect('website.com/userole');
      exit(); 
   } 
 } 
add_action('init', 'redirect_users_by_role');

We are now hooking into the init action as WordPress tells us that we have a fully authenticated user at this point.我们现在正在连接init操作,因为 WordPress 告诉我们此时我们有一个完全经过身份验证的用户。 We then check that the current page is the home page using is_page('home') and then do your redirection logic if needed.然后我们使用is_page('home')检查当前页面是否是主页,然后根据需要执行重定向逻辑。

It is not tested as I don't have a WordPress installation to hand so let me know if it works.它没有经过测试,因为我手头没有 WordPress 安装,所以让我知道它是否有效。

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

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