wordpress - Wordpress redirect based on user role and specific page ID
问题描述
I'm trying to create a function for a WordPress site that basically says, if the user has a role called LinkedIn and tries to access any page that is not page ID #6, then I need to redirect them to a different page.
Here is my code below, but it doesn't work and I cant figure out why... what am I missing?
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/');
}
}
}
And how my custom role is registered:
add_role( 'linkedin', __( 'LinkedIn' ), array( 'read' => true, ) );
Your help is so appreciated!
解决方案
You have to add exit
after using wp_redirect. 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();
}
}
}
推荐阅读
- azure - Azure Pipelines 中的 Glob 模式以排除地图文件和资产文件夹
- python - 在 Lambda Python 中使用两个变量
- javascript - 如何只为数组中的第一项做某事
- java - @Consume 如何填充变量和对象?
- azure-devops - 代理服务在 VM 上运行,但在 DevOps 门户中显示为脱机
- javascript - JavaScript 映射对象
- r - 当我在 R 中多次运行代码时更新 csv
- typescript - 在打字稿中加载类属性
- java - 如何在 Firebase 实时数据库中检索值
- node.js - 如何从 npm 包中获取 *.min.js 文件