在WordPress中为自定义文章类型添加功能

7
我正在创建自定义插件,希望能够动态地按用户角色为特定的自定义文章类型添加功能。
$args = array(
    'labels'             => $labels,
    'description'        => __( 'Description.', 'elementor-siteset' ),
    'public'             => true,
    'publicly_queryable' => true,
    'show_ui'            => true,
    'show_in_menu'       => true,
    'query_var'          => true,
    'rewrite'            => array( 'slug' => $this->slug ),
    'capability_type'    => 'member',
    'has_archive'        => true,
    'hierarchical'       => false,
    'menu_position'      => null,
    'supports'           => array( 'title'),
    'menu_icon'          => 'dashicons-groups',
    'exclude_from_search' => false,
    'can_export' => true,
    'capabilities' => array(
        'edit_post' => 'edit_member',
        'edit_posts' => 'edit_members',
        'edit_others_posts' => 'edit_other_members',
        'publish_posts' => 'edit_members',
        'read_post' => 'read_member',
        'read_private_posts' => 'read_private_members',
        'delete_post' => 'delete_member',
        'create_posts' => 'edit_members'
    ),
    'map_meta_cap' => true
);

这是我的WordPress后台。
我希望能够实现动态保存。如果我从后台保存,则应为特定CPT的特定角色保存功能。一旦我解决了以下问题,我将解决此问题。
我想做到这一点。以下代码仅用于测试。我只想为成员CPT添加读取、编辑和删除功能。如何实现?
function add_theme_caps() {
    // gets the administrator role
    $admins = get_role( 'editor' );

    $admins->add_cap( 'edit_member' ); 
    $admins->add_cap( 'edit_members' ); 
    $admins->add_cap( 'edit_other_members' ); 
    $admins->add_cap( 'publish_members' ); 
    $admins->add_cap( 'delete_member' ); 
    $admins->add_cap( 'read_member' ); 
    $admins->add_cap( 'read_private_members' ); 
}
add_action( 'admin_init', 'add_theme_caps', 10,2);

上述代码是为特定角色删除整个CPT“Member”。 如果我添加remove_cap()来删除任何功能,则不会按预期影响。
1个回答

2
这段代码对我有效。最初的回答。
add_action( 'init', 'register_cpt_gallery' );

function register_cpt_gallery() {
$labels = array( 
'name' => _x( 'Galleries', 'gallery' ),
'singular_name' => _x( 'Gallery', 'gallery' ),
'add_new' => _x( 'Add New', 'gallery' ),
'add_new_item' => _x( 'Add New Gallery', 'gallery' ),
'edit_item' => _x( 'Edit Gallery', 'gallery' ),
'new_item' => _x( 'New Gallery', 'gallery' ),
'view_item' => _x( 'View Gallery', 'gallery' ),
'search_items' => _x( 'Search Galleries', 'gallery' ),
'not_found' => _x( 'No galleries found', 'gallery' ),
'not_found_in_trash' => _x( 'No galleries found in Trash', 'gallery' ),
'parent_item_colon' => _x( 'Parent Gallery:', 'gallery' ),
'menu_name' => _x( 'Galleries', 'gallery' ),
);

$args = array( 
  'labels' => $labels,
  'hierarchical' => true,
  'description' => 'Image galleries for teachers classes',
  'supports' => array( 'title', 'editor', 'author'),
  'public' => true,
  'show_ui' => true,
  'show_in_menu' => true,
  'menu_icon' => get_bloginfo('template_url') . '/images/imagegallery.png',
  'show_in_nav_menus' => true,
  'publicly_queryable' => true,
  'exclude_from_search' => false,
  'has_archive' => true,
  'query_var' => true,
  'can_export' => true,
  'rewrite' => true,
  'capabilities' => array(
    'edit_post' => 'edit_gallery',
    'edit_posts' => 'edit_galleries',
    'edit_others_posts' => 'edit_other_galleries',
    'publish_posts' => 'publish_galleries',
    'read_post' => 'read_gallery',
    'read_private_posts' => 'read_private_galleries',
    'delete_post' => 'delete_gallery'
  ),
// as pointed out by iEmanuele, adding map_meta_cap will map the meta correctly 
'map_meta_cap' => true
);

register_post_type( 'gallery', $args );
}

为了让权限在后台实际生效,包括“管理员”,额外的功能应该添加到角色中,例如:

最初的回答:

function add_theme_caps() {
// gets the administrator role
$admins = get_role( 'administrator' );

$admins->add_cap( 'edit_gallery' ); 
$admins->add_cap( 'edit_galleries' ); 
$admins->add_cap( 'edit_other_galleries' ); 
$admins->add_cap( 'publish_galleries' ); 
$admins->add_cap( 'read_gallery' ); 
$admins->add_cap( 'read_private_galleries' ); 
$admins->add_cap( 'delete_gallery' ); 
}
add_action( 'admin_init', 'add_theme_caps');

我希望这对你有所帮助。 谢谢。 最初的回答:

我希望这对你有所帮助。

谢谢。


感谢给我时间,但我已经尝试过了。这段代码不仅提供读取能力,而且只提供特定角色的编辑和删除能力。如果您有任何解决方案,只给特定角色提供阅读、编辑、删除和添加能力,请告诉我。我已经尝试过所有方法了。 - Mayank Dudakiya
这里是删除代码,请查看以下链接:https://stackoverflow.com/questions/33701343/wordpress-capabilities-custom-post-type-delete-post-is-not-working - Full Stop
根据我的问题,我正在寻找完整的读取、写入、删除和编辑功能。我也检查了大多数插件,它们并没有提供相同的功能。 - Mayank Dudakiya

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接