自定义用户角色不使用自定义帖子类型

时间:2016-03-21 作者:steamfunk

我目前正在制作一个自定义用户角色“adoption\\u agency”。除了这个自定义角色之外,我还创建了一个名为“收养”的自定义帖子类型。我已经授予“administrators”和“shop\\u manager”对此帖子类型的完全访问权限,这应该允许他们删除、更新或编辑帖子。当我以“adoption\\u agency”、“shop\\u manager”或“administrator”用户身份登录时,我可以查看dashbaord上的自定义帖子类型,并可以访问它。我面临的问题是,当我加入新的收养并发布它时,它会说“你不允许编辑这篇文章。”我希望“adoption\\u agency”用户的仪表盘上只能显示“adoptions”帖子类型。我已经在下面添加了我的代码,我已经尝试了几天了,我似乎无法让它工作。我真的很感激你的帮助。

以下是我的自定义帖子类型代码:

// Register Custom Post Type
function custom_post_type_adoption() {

    $labels = array(
        \'name\'                  => _x( \'adoptions\', \'Post Type General Name\', \'text_domain\' ),
        \'singular_name\'         => _x( \'adoption\', \'Post Type Singular Name\', \'text_domain\' ),
        \'menu_name\'             => __( \'Adoptions\', \'text_domain\' ),
        \'name_admin_bar\'        => __( \'Adoptions\', \'text_domain\' ),
        \'archives\'              => __( \'Adoption Archives\', \'text_domain\' ),
        \'parent_item_colon\'     => __( \'Parent Item:\', \'text_domain\' ),
        \'all_items\'             => __( \'All Adoptions\', \'text_domain\' ),
        \'add_new_item\'          => __( \'Add New Adoption\', \'text_domain\' ),
        \'add_new\'               => __( \'Add New Adoption\', \'text_domain\' ),
        \'new_item\'              => __( \'New Adoption\', \'text_domain\' ),
        \'edit_item\'             => __( \'Edit Adoption\', \'text_domain\' ),
        \'update_item\'           => __( \'Update Adoption\', \'text_domain\' ),
        \'view_item\'             => __( \'View Adoption\', \'text_domain\' ),
        \'search_items\'          => __( \'Search Adoption\', \'text_domain\' ),
        \'not_found\'             => __( \'Adoption Not found\', \'text_domain\' ),
        \'not_found_in_trash\'    => __( \'Adoption Not found in Trash\', \'text_domain\' ),
        \'featured_image\'        => __( \'Featured Image\', \'text_domain\' ),
        \'set_featured_image\'    => __( \'Set featured image\', \'text_domain\' ),
        \'remove_featured_image\' => __( \'Remove featured image\', \'text_domain\' ),
        \'use_featured_image\'    => __( \'Use as featured image\', \'text_domain\' ),
        \'insert_into_item\'      => __( \'Insert into Adoption\', \'text_domain\' ),
        \'uploaded_to_this_item\' => __( \'Uploaded to this Adoption\', \'text_domain\' ),
        \'items_list\'            => __( \'Adoption list\', \'text_domain\' ),
        \'items_list_navigation\' => __( \'Items list navigation\', \'text_domain\' ),
        \'filter_items_list\'     => __( \'Filter items list\', \'text_domain\' ),
    );
    $args = array(
        \'label\'                 => __( \'adoption\', \'text_domain\' ),
        \'description\'           => __( \'Post type for Adoptions\', \'text_domain\' ),
        \'labels\'                => $labels,
        \'supports\'              => array( \'title\', \'excerpt\', \'author\', \'thumbnail\', ),
        \'hierarchical\'          => false,
        \'public\'                => true,
        \'show_ui\'               => true,
        \'show_in_menu\'          => true,
        \'menu_position\'         => 5,
        \'menu_icon\'             => \'dashicons-smiley\',
        \'show_in_admin_bar\'     => true,
        \'show_in_nav_menus\'     => true,
        \'can_export\'            => true,
        \'has_archive\'           => true,        
        \'exclude_from_search\'   => false,
        \'publicly_queryable\'    => true,
        \'capability_type\' => array("adoption", "adoptions"),
    );
    register_post_type( \'adoptions\', $args );

}
add_action( \'init\', \'custom_post_type_adoption\', 0 );
以下是我的自定义用户角色代码:

function add_adoption_caps_to_admin() {
  $caps = array(
    \'read\',
    \'read_adoptions\',
    \'read_private_adoptions\',
    \'edit_adoptions\',
    \'edit_private_adoptions\',
    \'edit_published_adoptions\',
    \'edit_others_adoptions\',
    \'publish_adoptions\',
    \'delete_adoptions\',
    \'delete_private_adoptions\',
    \'delete_published_adoptions\',
    \'delete_others_adoptions\',
    \'upload_files\',
    \'edit_files\',

  );
  $roles = array(
    get_role( \'administrator\' ),
    get_role( \'editor\' ),
    get_role( \'shop_manager\' ),
  );
  foreach ($roles as $role) {
    foreach ($caps as $cap) {
      $role->add_cap( $cap );
    }
  }
}
add_action( \'after_setup_theme\', \'add_adoption_caps_to_admin\' );


function adoption_agency_setup() {
  add_role( \'adoption_agency\', \'Adoption Agency\');
}
add_action( \'after_setup_theme\', \'adoption_agency_setup\' );




function add_adoption_caps_to_non_admin_roles() {
  # Everyone gets these capabilities:
  $caps = array(
    \'read\',
    \'read_adoptions\',
    \'read_private_adoptions\',
    \'edit_adoptions\',
    \'edit_published_adoptions\',
    \'publish_adoptions\',
    \'delete_adoptions\',
    \'delete_published_adoptions\',
    \'upload_files\',
    \'edit_files\',
  );
  $roles = array(

    get_role( \'adoption_agency\' ),

  );
  foreach ($roles as $role) {
    foreach ($caps as $cap) {
      $role->add_cap( $cap );
    }
  }
}
add_action( \'after_setup_theme\', \'add_adoption_caps_to_non_admin_roles\' );




function custom_remove_menus(){

    // Get current user\'s data
    $current_user = wp_get_current_user();
    $user_id = $current_user->ID;

    // Check user\'s roles
    $user = new WP_User( $user_id );
    if ( !empty( $user->roles ) && is_array( $user->roles ) ) {
        if( in_array( \'adoption_agency\', $user->roles ) ) {
            // Remove menu items
            remove_menu_page( \'upload.php\');
            remove_menu_page( \'index.php\');

        }
    }
}

add_action( \'admin_menu\', \'custom_remove_menus\' );

1 个回复
SO网友:Nathan Powell

我想我会尽量澄清我的评论:

你只从设置capability_types 上面的方式,在Note 2 属于capabilities.

这是我看到的$GLOBALS[\'wp_post_types\'][\'adoptions\']->cap:

stdClass Object(
    [edit_post] => edit_adoption
    [read_post] => read_adoption
    [delete_post] => delete_adoption
    [edit_posts] => edit_adoptions
    [edit_others_posts] => edit_others_adoptions
    [publish_posts] => publish_adoptions
    [read_private_posts] => read_private_adoptions
    [create_posts] => edit_adoptions
)
一旦我添加\'map_meta_cap\' => true, 到您的帖子类型$args 我得到了一个更完整的对象:

stdClass Object(
    [edit_post] => edit_adoption
    [read_post] => read_adoption
    [delete_post] => delete_adoption
    [edit_posts] => edit_adoptions
    [edit_others_posts] => edit_others_adoptions
    [publish_posts] => publish_adoptions
    [read_private_posts] => read_private_adoptions
    [read] => read
    [delete_posts] => delete_adoptions
    [delete_private_posts] => delete_private_adoptions
    [delete_published_posts] => delete_published_adoptions
    [delete_others_posts] => delete_others_adoptions
    [edit_private_posts] => edit_private_adoptions
    [edit_published_posts] => edit_published_adoptions
    [create_posts] => edit_adoptions
)

相关推荐

如何从USERS-edit.php中删除颜色选择器代码

我知道我不应该改变Wordpress的核心。但如果我想在“用户编辑”中使用仪表板用户配置文件页面。php和删除大块代码(如颜色选择器)的方法。从第259行到第336行-我想全部删除。<?php if ( ! ( IS_PROFILE_PAGE && ! $user_can_edit ) ) : ?> <tr class="user-rich-editing-wrap"> <th scope="