Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基于用户和当前页面函数重定向。php_Php_Wordpress_Function_Redirect - Fatal编程技术网

基于用户和当前页面函数重定向。php

基于用户和当前页面函数重定向。php,php,wordpress,function,redirect,Php,Wordpress,Function,Redirect,我正在寻找一段代码添加到我的wordpress站点,根据用户的角色重定向某些用户,但仅当他们到达某个页面时 我需要它向特定的用户角色显示特定的页面,而不是其他人得到的页面。插件在我的场景中不起作用 我不仅仅是在php中寻找重定向。 我需要通过三个步骤来制作一些东西: 首先,我需要检查用户/访问者是否在所需页面上 在那之后,我需要检查一个特定的用户角色(我已经涵盖了这一点) 最后重新定向(把盖子盖到) 我不知道如何在functions.php中完成第一步,也不知道我的流程是否合理 编辑:解决了 a

我正在寻找一段代码添加到我的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'); 
    } 
}

首先,您需要获得用户角色

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


您试过什么吗?你能给你已经尝试过的东西添加一些代码吗?下面可能是我在某个页面上尝试重定向的一个草率片段的重复,但遗憾的是,它什么也没做。这可能是因为我打电话回家的方式不对。然而,我不知道该怎么称呼它<代码>@JorelLokiAmthor不是。谢谢。不过,这段代码似乎很完美。。为了使它适合functions.php,它不应该在一个动作挂钩中吗?如果是这样的话,你知道在哪里使用$tag吗?例如:
add_操作('$tag','redirect_user_role')添加操作('template_redirect','hooker');函数hooker(){//I在选择模板并将其呈现到屏幕之前加载}我使用的最终代码:
add_action('template_redirect','redirect_user_role');函数redirect_user_role(){if(当前_user_can('subscriber')&&is_页面('ID,Slug或name here')){wp_redirect('https://www.example.nl“);}}
您得到输出了吗