基于用户角色和特定页面ID的Wordpress重定向



我正在尝试为WordPress网站创建一个函数,如果用户有一个名为LinkedIn的角色并试图访问任何非的页面页ID #6,然后我需要重定向到另一个页面。

下面是我的代码,但它不起作用,我不知道为什么…我错过了什么?


add_action('template_redirect', 'redirect_user_role');  
function redirect_user_role() {      
$user = wp_get_current_user();
if ( in_array( 'linkedin', (array) $user->roles ) ) {
if(!is_page( 6 ))   {           
wp_redirect('/no-access/');     
} 
}
}

以及如何注册我的自定义角色:

add_role( 'linkedin', __( 'LinkedIn' ), array( 'read' => true, ) ); 

非常感谢你的帮助!

您必须在使用wp_redirect后添加exit。https://developer.wordpress.org/reference/functions/wp_redirect/

add_action('template_redirect', 'redirect_user_role');
function redirect_user_role() {
$user = wp_get_current_user();
if ( in_array( 'linkedin', (array) $user->roles ) ) {
if(!is_page( 6 ))   {
wp_redirect('/no-access/');
exit();
}
}
}

最新更新