自定义角色和功能介质不能正常工作

时间:2019-05-20 作者:Barbara Ester

帮助新用户自定义wordpress功能和角色。媒体无法在galery中上载和显示图像。

图像错误:https://i.imgur.com/1bPbcAm.png

代码:

if ( get_role(\'user_products\') ) {
    remove_role(\'user_products\');
}

add_action(\'init\',\'add_my_custom_role\');
function add_my_custom_role() {

    add_role( 
      \'user_products\', 
      __( \'User Products\', \'editproducts\' ), 
      array(
               \'read\'            => true,
               \'create_posts\'      => true,
               \'edit_posts\'        => true,
               \'edit_others_posts\' => true,
               \'delete_posts\' => true,
               \'upload_files\' => true,
               \'upload_medias\' => true,                   
               \'edit_files\' => true,
               \'publish_posts\' => true,
               \'manage_categories\' => true,
               \'edit_comment\' => true,
               \'edit_products\' => true,
               \'edit_others_products\' => true,
               \'publish_products\' => true,
               \'delete_products\' => true,
               \'read_private_products\' => true,
               \'delete_private_products\' => true,
               \'edit_private_products\' => true,
               \'edit_published_products\' => true,
               \'delete_others_pages\' => true,
               \'delete_others_posts\' => true,
               \'delete_pages\' => true,
               \'delete_private_pages\' => true,
               \'delete_private_posts\' => true,
               \'delete_published_pages\' => true,
               \'delete_published_posts\' => true,
               \'edit_others_pages\' => true,
               \'edit_pages\' => true,
               \'edit_private_pages\' => true,
               \'edit_private_posts\' => true,
               \'edit_published_pages\' => true,
               \'edit_published_posts\' => true,
               \'manage_links\' => true,
               \'moderate_comments\' => true,
               \'publish_pages\' => true,
               \'read_private_pages\' => true,
               \'read_private_posts\' => true,
               \'unfiltered_html\' => false,
               \'level_3\' => true,                   
              // \'edit_attachments\' => true,
              // \'delete_attachments\' => true,
              // \'read_others_attachments\' => true,
              // \'edit_others_attachments\' => true,
              // \'delete_others_attachments\' => true,

        // Various Capabilities
    ));
}
代码更新,我有相同的错误:

add_action(\'init\',\'add_my_custom_role\');
function add_my_custom_role() {

    add_role( 
      \'user_products\', 
      __( \'User Products\', \'editproducts\' ), 
      array(
               \'read\'            => true,
               \'create_posts\'      => true,
               \'edit_posts\'        => true,
               \'edit_others_posts\' => true,
               \'delete_posts\' => true,
               \'upload_files\' => true,
               \'upload_medias\' => true,                   
               \'edit_files\' => true,
               \'publish_posts\' => true,
               \'manage_categories\' => true,
               \'edit_comment\' => true,
               \'edit_products\' => true,
               \'edit_others_products\' => true,
               \'publish_products\' => true,
               \'delete_products\' => true,
               \'read_private_products\' => true,
               \'delete_private_products\' => true,
               \'edit_private_products\' => true,
               \'edit_published_products\' => true,
               \'delete_others_pages\' => true,
               \'delete_others_posts\' => true,
               \'delete_pages\' => true,
               \'delete_private_pages\' => true,
               \'delete_private_posts\' => true,
               \'delete_published_pages\' => true,
               \'delete_published_posts\' => true,
               \'edit_others_pages\' => true,
               \'edit_pages\' => true,
               \'edit_private_pages\' => true,
               \'edit_private_posts\' => true,
               \'edit_published_pages\' => true,
               \'edit_published_posts\' => true,
               \'manage_links\' => true,
               \'moderate_comments\' => true,
               \'publish_pages\' => true,
               \'read_private_pages\' => true,
               \'read_private_posts\' => true,
               \'unfiltered_html\' => false,
               \'level_3\' => true,                   
              // \'edit_attachments\' => true,
              // \'delete_attachments\' => true,
              // \'read_others_attachments\' => true,
              // \'edit_others_attachments\' => true,
              // \'delete_others_attachments\' => true,

        // Various Capabilities
    ));
}

1 个回复
SO网友:Tom J Nowell

这是因为在每次调用页面加载时remove_role

如文件所述:

删除角色后,具有此角色的用户将失去该站点上的所有权限。

角色与帖子类型不同,它们存在于数据库中,删除/添加角色会更新数据库。因此,您不应该在每次页面加载时都添加它们,最好在主题激活或插件激活时添加

相关推荐

Plugin capabilities

我创建了一个界面,允许用户执行一定数量的操作,每个操作都有相关的功能(包括访问该界面)。我想有一个基本的能力(这里是原始的?)如果用户想要执行所有其他操作,则需要具备:如果用户可以删除项目,则用户还必须能够访问该界面。有几个插件允许添加/删除功能:我想防止出现用户可以删除但无法访问界面的情况。我知道,由于帖子类型、元功能和map\\u meta\\u cap filter,我很难将其应用到我的插件中。我可以一直检查这两个选项,但权限层次结构最终会更加复杂(比如,3个级别-用户必须具有访问\\u界面、创建\