基于用户和当前页面功能的重定向.php

时间:2016-01-28 15:54:24

标签: php wordpress function redirect

我正在寻找一段代码添加到我的wordpress网站,根据他们的角色重定向某些用户,但只有当他们到达某个页面时。

我需要这个来显示特定用户角色的特定页面,而不是其他人获得的页面。插件在我的场景中不起作用。

我不只是在php中寻找重定向。 我需要制作一些有效的方法:

首先,我需要检查用户/访客是否在所需页面上。

之后我需要检查一个特定的用户角色(我已经覆盖了这个)。

最后重定向(得到了覆盖)。

我不知道如何完成functions.php中的第一步,如果我的流程有意义......

编辑:解决了!

add_action('template_redirect', 'redirect_user_role'); 

function redirect_user_role()
{ 
    if(current_user_can('subscriber') && is_page(' ID, Slug or name here ')) 
    { 
        wp_redirect('https://www.example.nl'); 
    } 
}

1 个答案:

答案 0 :(得分:0)

首先,你需要获得用户的角色,

  <?php
    global $current_user, $wpdb;
    $role = $wpdb->prefix . 'capabilities';
    $current_user->role = array_keys($current_user->$role);
    $role = $current_user->role[0]; //user role
    $page_title = $post->post_title; //get title  f page
    if($role=='subscriber' &&  $page_title=="aboutus") // compare
    { 
         wp_redirect('http://www.google.com'); //navigate if so
    }
   ?>