代码之家  ›  专栏  ›  技术社区  ›  Lieutenant Dan

在自定义用户角色中添加、编辑、删除页面功能

  •  0
  • Lieutenant Dan  · 技术社区  · 7 年前

    试图将能力添加到 添加、编辑和删除页面 '在WordPress自定义用户角色中。

    我正在下面创建一个自定义用户角色,即' 副管理员 '.我试图授予所有“页面”能力的访问权限;但即使识别了下面的内容,它也不起作用。(无“添加页面”,编辑当前页面选项卡显示)。

    也许还需要注意;我是从一个自定义的儿童主题 /function.php 文件。角色在wp仪表板中显示在下面的代码之后(即sub-admin),但是我已经 允许访问页面失败。

    add_role(
        'sub_admin',
        __( 'Sub Admin' ),
        array(
            'read'         => true,  
            'edit_posts'   => true,
            'publish_posts' => true,
            'edit_pages'   => true,
            'edit_others_pages' => true,
            'publish_page' => true,
            'edit_pages'=>true,
            'edit_published_pages'=>true,
            'publish_pages'=>true,
            'delete_pages'=>true,
            'delete_others_pages'=>true,
            'delete_published_pages'=>true,
        )
    );
    
    0 回复  |  直到 7 年前
        1
  •  0
  •   user8554313    7 年前

    请尝试以下方法来解决您的问题

    请确保您的角色是通过从 add_role

    $result = add_role(/*your args*/);
    if ( null !== $result ) {
      echo 'Yay! New role created!';
    }
    else {
          echo 'Oh... the basic_contributor role already exists.';
    }
    

    如果不好。请确保,当您试图编辑页面时,您具有此子管理员角色

    如果问题仍然存在,请注意WordPress开发者指南通知

    在第一次调用add_role()之后,角色及其功能 将存储在数据库中!

    顺序调用将不起任何作用:包括更改功能 列表,这可能不是您期望的行为。

    也许你需要 remove_role() 然后 附加角色 再一次。 也许你第一次创建角色时没有一些能力。

    另外,请尝试使用init操作添加角色

    function wporg_simple_role()
    {
        remove_role('sub_admin');
        // or add_role('sub_admin');
    }
    
    
    add_action('init', 'wporg_simple_role');
    

    可能没有编辑角色,所有的管理员功能是

    activate_plugins
    delete_others_pages
    delete_others_posts
    delete_pages
    delete_posts
    delete_private_pages
    delete_private_posts
    delete_published_pages
    delete_published_posts
    edit_dashboard
    edit_others_pages
    edit_others_posts
    edit_pages
    edit_posts
    edit_private_pages
    edit_private_posts
    edit_published_pages
    edit_published_posts
    edit_theme_options
    export
    import
    list_users
    manage_categories
    manage_links
    manage_options
    moderate_comments
    promote_users
    publish_pages
    publish_posts
    read_private_pages
    read_private_posts
    read
    remove_users
    switch_themes
    upload_files
    customize
    delete_site
    

    也许管理栏也取决于管理选项。 您可以通过选中“不是管理页”来删除“管理选项”功能

    function change_role() {
        global $wp_roles;
    
        if ( ! is_admin() ) {
            return;
        }
    
        // if not admin page - you could temporary remove manage capabilities from 
        // sub_admin role
    
    
    }
    
    add_action('wp', 'change_role');