【问题标题】:PHP - What's opposite to current_user_can?PHP - current_user_can 的对立面是什么?
【发布时间】:2016-02-27 04:39:30
【问题描述】:

我正在尝试做与此相反的事情......

function my_custom_init() {
    if (current_user_can('administrator')) { 
        remove_post_type_support( 'company', 'editor' );
        remove_post_type_support( 'company', 'excerpt' );
    }
}
add_action( 'init', 'my_custom_init' );

又名:如果当前用户不是“管理员”,则删除帖子支持。

【问题讨论】:

标签: php wordpress


【解决方案1】:

只需用! 否定条件即可:

function my_custom_init() {
    if (!current_user_can('administrator')) { 
        remove_post_type_support( 'company', 'editor' );
        remove_post_type_support( 'company', 'excerpt' );
    }
}
add_action( 'init', 'my_custom_init' );

【讨论】:

  • 角色,是的,我的错。您仍然可以使用 current_user_can() / user_can() 来检查用户是否具有该角色
  • @IgorYavych 是的,对不起;刚刚意识到并修复了它。
【解决方案2】:

尝试使用 not 运算符 (!),如下所示:

function my_custom_init() {
   if (!current_user_can('administrator') { 
       remove_post_type_support( 'company',     'editor' );       
    remove_post_type_support( 'company', 'excerpt' ); 
    }
} 

add_action( 'init', 'my_custom_init' );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-23
    • 2021-03-19
    • 2013-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多