问题描述
我正在寻找一段代码添加到我的wordpress网站中,以便根据某些用户的角色重定向他们,但是只有当他们进入某个页面时.
I'm looking for a piece of code to add to my wordpress site to redirect certain users based on their role but only when they get to a certain page.
我需要这样做才能向特定用户角色显示特定页面,而不是其他用户.在我的情况下,插件无法使用.
I need this to show a specific page to a specific user role and not the one others get. A plugin won't do in my scenario.
我不仅在php中寻找重定向.我需要做一些可以在3个步骤中起作用的东西:
I'm not just looking for a redirect in php.I need to make something that will work in 3 steps:
首先,我需要检查用户/访问者是否在所需页面上.
First I need to check if the user/visitor is on the desired page.
在那之后,我需要检查特定的用户角色(我已经了解了).
After that I need to check for a specific user role (I got this covered).
最后重定向(覆盖了).
And at last to redirect (got that coverd to).
我不知道如何完成functions.php的第一步,以及我的流程是否有意义.
I have no idea how to accomplish the first step inside the functions.php and if my flow even makes sense..
已解决!
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');
}
}
推荐答案
首先,您需要获得用户角色,
First you need to get role of user,
<?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
}
?>
这篇关于根据用户和当前页面的功能进行重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!