【问题标题】:ACF Radio Buttons to Add and Remove User Role Capabilities isn't Removing the Capabilities用于添加和删除用户角色功能的 ACF 单选按钮不会删除功能
【发布时间】: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


    【解决方案1】:

    我不确定这是否是问题所在,但请尝试在这些 if / elseif 块上使用 == 或 === comparison operators。按照您现在编写的方式,它总是会分配“是”到 $january 所以它永远不会到达 elseif 块。

        if ($january = 'yes') {
    
          $restaurant->add_cap( 'edit_january_offer' ); 
          $restaurant->add_cap( 'delete_january_offer' ); 
          $restaurant->add_cap( 'edit_january_offers' ); 
    
    
      } elseif ($january = 'no') {
    

    会变成:

        if ($january == 'yes') {
    
          $restaurant->add_cap( 'edit_january_offer' ); 
          $restaurant->add_cap( 'delete_january_offer' ); 
          $restaurant->add_cap( 'edit_january_offers' ); 
    
    
      } elseif ($january == 'no') {
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-25
      • 1970-01-01
      • 1970-01-01
      • 2012-12-23
      • 1970-01-01
      相关资源
      最近更新 更多