我想限制用户在Wordpress后端的访问权限,使该用户只能(读、写)访问一个类别的帖子。用户甚至不应该能够在后端读取来自另一个类别的帖子。
我如何做到这一点(理想情况下是通过向functions.php添加一些代码),并且不影响任何性能(例如页面加载)?
我基本上是想在这个插件过时之前实现它的功能:https://wordpress.org/plugins/restrict-categories/
"限制类别是一个插件,允许您选择用户可以在帖子编辑屏幕中查看、添加和编辑的类别。此插件允许您根据用户角色和用户名限制访问。"
如果你不介意使用最新的插件,那么这个插件可以满足你的所有要求,并且已经定期更新,有40000多个活动安装。
用户访问管理器
https://wordpress.org/plugins/user-access-manager/
2020年更新,工作答案,我找到了不同的答案,现在在新的WordPress中都不工作。
function hide_categories_for_specific_user( $exclusions, $args ){
if ( ((defined( 'REST_REQUEST' ) && REST_REQUEST) or $GLOBALS['pagenow'] === 'edit.php' ) && !current_user_can( 'manage_options' ) ) {
// IDs of terms to be excluded
$exclude_array = array("12","16","17"); // CHANGE THIS TO IDs OF YOUR TERMS
// Generation of exclusion SQL code
$exterms = wp_parse_id_list( $exclude_array );
foreach ( $exterms as $exterm ) {
if ( empty($exclusions) )
$exclusions = ' AND ( t.term_id <> ' . intval($exterm) . ' ';
else
$exclusions .= ' AND t.term_id <> ' . intval($exterm) . ' ';
}
// Closing bracket
if ( !empty($exclusions) )
$exclusions .= ')';
// Return our SQL statement
}
return $exclusions;
}
// Finally hook up our filter
add_filter( 'list_terms_exclusions', 'hide_categories_for_specific_user', 10, 2 );
回答文章编辑器中隐藏某些类别的最终帮助