Reputation: 360
I have created a custom user role called restaurant_owner. I want to have a section in the admin where a logged in administrator can add or remove custom post type capabilities for that user role.
I have created my user role here:
//Create Restaurant Owner
add_action('init', 'restaurant_owner_user_role');
function restaurant_owner_user_role() {
add_role('restaurant_owner', 'Restaurant Owner');
}
I have then used ACF to create an options section, in this section I have added in multiple radio buttons. If the capability is ticked as yes then I want the capabilities to be added to the user (this works), when the admin clicks no I want the capabilities to be removed (this doesn't work).
//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');
What am I doing wrong? I can't workout why it won't remove the capabilities.
Here is an example of the admin area where they select yes or no for the multiple offers. Hopefully this helps to explain it.
Upvotes: 0
Views: 145
Reputation: 1
I'm not sure if this is the issue, but try using the == or === comparison operators on these if / elseif blocks..The way you have it written now, it will always assign 'yes' to $january so it will never reach the elseif block.
if ($january = 'yes') {
$restaurant->add_cap( 'edit_january_offer' );
$restaurant->add_cap( 'delete_january_offer' );
$restaurant->add_cap( 'edit_january_offers' );
} elseif ($january = 'no') {
would become:
if ($january == 'yes') {
$restaurant->add_cap( 'edit_january_offer' );
$restaurant->add_cap( 'delete_january_offer' );
$restaurant->add_cap( 'edit_january_offers' );
} elseif ($january == 'no') {
Upvotes: 0