0

我有我编写的名为 MyPlugin 的插件。我有一个是或否单选按钮,如果选项是是意味着它应该允许贡献者上传图片,而不是意味着它不应该允许。

这是允许贡献者上传图片的代码

if ( current_user_can('contributor') && !current_user_can('upload_files') )
    add_action('admin_init', 'allow_contributor_uploads');
function allow_contributor_uploads() {
    $contributor = get_role('contributor');
    $contributor->add_cap('upload_files');
} 
// this is the code to remove if the capabilities if it is added

if ( current_user_can('contributor') && current_user_can('upload_files') )
    add_action('admin_init', 'remove_contributor_upload');
function remove_contributor_upload(){
    $con = get_role('contributor');
    $con->remove_cap('upload_files');
} 

我需要帮助我应该把这段代码放在插件的哪里,我试过了,但我得到了错误

Error in wp-includes/capabilities.php on line 1059
4

1 回答 1

1

您正在调用无法在 init() 之前调用的函数,因此它们是未定义的(具体而言,wp_get_current_user() 是在 wp-includes/pluggable.php 中定义的,并且在加载所有插件之后才会加载)。在调用 admin_init,您需要重新排列代码以检查用户权限,例如

add_action('admin_init', 'allow_contributor_uploads');
function allow_contributor_uploads() {
    if ( current_user_can('contributor') && !current_user_can('upload_files') ) {
        $contributor = get_role('contributor');
        $contributor->add_cap('upload_files');
    }
}
// this is the code to remove if the capabilities if it is added

add_action('admin_init', 'remove_contributor_upload');
function remove_contributor_upload(){
    if ( current_user_can('contributor') && current_user_can('upload_files') ) {
        $con = get_role('contributor');
        $con->remove_cap('upload_files');
    }
}

注意:只是在这里解决你的错误问题,没有看过你在做什么的逻辑!

于 2013-02-07T09:43:03.003 回答