【发布时间】:2022-01-20 14:06:38
【问题描述】:
我正在尝试限制特定类别的产品针对特定用户角色进行编辑。知道如何在...wp-admin/post.php?post=1702&action=edit 中获取产品的类别 ID 吗?
到目前为止,这是我的代码:
function restrict_edit_if_in_manufacturing_cat($post){
global $pagenow, $post_type;
// Targeting admin edit single product screen
if ($pagenow === 'post.php' && $post_type === 'product') {
// Get current user
$user = wp_get_current_user();
// Roles
$roles = (array) $user->roles;
// Roles to check
$roles_to_check = array('shop_manager'); // several can be added, separated by a comma
// Compare
$compare = array_diff($roles, $roles_to_check);
// Result is empty
if (empty($compare)) {
// $category_ids = ...Get Category IDs here...
// If 458 is in the category id list die
if (strpos($category_ids, '458') !== false) {
wp_die('cheatn');
} else {
// do something if needed
}
}
}
}
add_action('pre_get_posts', 'restrict_edit_if_in_manufacturing_cat', 10, 1);
【问题讨论】:
标签: php wordpress woocommerce wordpress-admin