【发布时间】:2020-06-27 18:54:37
【问题描述】:
我创建了一个名为 restaurant_owner 的自定义用户角色。我希望在管理员中有一个部分,登录管理员可以在其中添加或删除该用户角色的自定义帖子类型功能。
我在这里创建了我的用户角色:
//Create Restaurant Owner
add_action('init', 'restaurant_owner_user_role');
function restaurant_owner_user_role() {
add_role('restaurant_owner', 'Restaurant Owner');
}
然后我使用 ACF 创建了一个选项部分,在这个部分中我添加了多个单选按钮。如果该功能被勾选为“是”,那么我希望将这些功能添加给用户(这可行),当管理员单击“否”时,我希望删除这些功能(这不起作用)。
//Adds Restaurant Owner Custom Capabilities
$options = get_fields($post_id);
$january = get_field_object('january_offer', 'option', $options);
function add_rest_caps() {
$restaurant = get_role( 'restaurant_owner' );
$restaurant->add_cap('read');
$restaurant->add_cap( 'edit_restaurant' );
$restaurant->add_cap( 'delete_restaurant' );
$restaurant->remove_cap( 'read_restaurant' );
$restaurant->add_cap( 'publish_restaurants' );
$restaurant->add_cap( 'edit_restaurants' );
$restaurant->remove_cap( 'edit_others_restaurants' );
$restaurant->remove_cap( 'delete_restaurants' );
$restaurant->remove_cap( 'delete_others_restaurants' );
$restaurant->remove_cap( 'read_private_restaurants' );
$restaurant->remove_cap( 'manage_categories' );
if ($january = 'yes') {
$restaurant->add_cap( 'edit_january_offer' );
$restaurant->add_cap( 'delete_january_offer' );
$restaurant->add_cap( 'edit_january_offers' );
} elseif ($january = 'no') {
//this bit doesn't work
$restaurant->remove_cap( 'edit_january_offer' );
$restaurant->remove_cap( 'delete_january_offer' );
$restaurant->remove_cap( 'edit_january_offers' );
}
}
add_action( 'init', 'add_rest_caps');
我做错了什么?我无法确定为什么它不会删除这些功能。
这是一个管理区域的示例,他们在其中为多个优惠选择是或否。希望这有助于解释它。
【问题讨论】:
标签: wordpress advanced-custom-fields custom-post-type acfpro capability