根据用户和当前页面功能重定向.php


Redirect based on user and current page functions.php

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

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

我不只是在寻找 php 中的重定向。我需要制作一些可以通过 3 个步骤工作的东西:

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

之后,我需要检查特定的用户角色(我涵盖了这个)。

最后重定向(覆盖到)。

我不知道如何在函数中完成第一步.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'); 
    } 
}

首先,您需要获取用户的角色,

  <?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
    }
   ?>