如何为自定义角色赋予自定义文章类型的功能。

huangapple go评论82阅读模式
英文:

How to give Capabilities to a Custom Role for a Custom Post Type

问题

自定义角色"Student"无法与自定义文章类型"Event"一起工作,尽管我已经为"Event"自定义文章类型添加了特定的功能,但是使用当前的代码,自定义角色只能"查看"所有自定义文章类型,没有其他权限。我是通过插件添加了自定义角色,并且自定义文章类型的代码位于functions.php中。以下是我的代码:

自定义文章类型:

function mepoNET_post_types() {
    register_post_type('event', array(
        'public'            => true,
        'labels'            => array(
            'name'          => 'Events',
            'add_new_item'  => 'Add New Event',
            'edit_item'     => 'Edit Event',
            'all_items'     => 'All Events',
            'singular_name' => 'Event'
        ),
        'menu_icon'         =>'dashicons-calendar-alt',
        'supports'          => array('title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments'),
        'show_in_rest'      => true,
        'taxonomies'        => array('category', 'post_tag'),
        'has_archive'       => true,
        'capabilities'      => array('event', 'events'),
        'map_meta_cap'      => true,
    ));
}

add_action('init', 'mepoNET_post_types');

自定义角色插件:

defined('ABSPATH') or die();

register_activation_hook(__FILE__, 'mepoNET_roles_activaton');

register_deactivation_hook(__FILE__, 'mepoNET_roles_deactivaton');

function mepoNET_roles_activaton(){
    $caps = array(
        'read'         => true,
        'edit_posts'   => true,
        'upload_files' => true
    );

    add_role('mepo_student', 'Student', $caps);
}

function mepoNET_roles_deactivaton(){
    remove_role('mepo_student');
}

add_action('admin_init', 'mepoNET_add_role_caps', 999);

function mepoNET_add_role_caps(){
    $student = get_role('mepo_student');

    $student->add_cap('edit_event');
    $student->add_cap('edit_events');
    $student->add_cap('read_event');
    $student->add_cap('delete_event');
    $student->add_cap('publish_events');
    $student->add_cap('read_private_event');
    $student->add_cap('delete_event');
}

希望这能帮助你解决问题。

英文:

I am trying to give my custom role 'Student" capabilities so they can work with the custom post type 'Event'. With my current code the custom role can only "view" all the custom post types and has no other power, even though I have add capabilities specifically for the 'Event' custom post type. I have added the custom role using a plugin and the custom post type code lies in the functions.php. Here's my code:

Custom Post Type:

function mepoNET_post_types() {
            register_post_type('event', array(
                'public'            => true,
                'labels'            => array(
                    'name'          => 'Events',
                    'add_new_item'  => 'Add New Event',
                    'edit_item'     => 'Edit Event',
                    'all_items'     => 'All Events',
                    'singular_name' => 'Event'
                ),
                'menu_icon'         =>'dashicons-calendar-alt',
                'supports'          => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' ),
                'show_in_rest'      => true,
                'taxonomies'        => array( 'category', 'post_tag' ),
                'has_archive'       => true,
                'capabilities'      => array('event', 'events'),
                'map_meta_cap'      => true,
            ));
}
        
        add_action('init', 'mepoNET_post_types');

Custom Roles Plugin:

 
 defined('ABSPATH') or die();

 
 register_activation_hook(__FILE__,'mepoNET_roles_activaton');


register_deactivation_hook(__FILE__,'mepoNET_roles_deactivaton');

 function mepoNET_roles_activaton(){
    $caps =[
        'read'         => true,
        'edit_posts'   => true,
        'upload_files' => true
    ];

    add_role('mepo_student', 'Student', $caps);
 }

function mepoNET_roles_deactivaton(){
    remove_role('mepo_student');
}

add_action('admin_init', 'mepoNET_add_role_caps', 999);

function mepoNET_add_role_caps(){
    $student = get_role( 'mepo_student' );

    $student->add_cap( 'edit_event' ); 
    $student->add_cap( 'edit_events' ); 
    $student->add_cap( 'read_event' ); 
    $student->add_cap( 'delete_event' ); 
    $student->add_cap( 'publish_events' ); 
    $student->add_cap( 'read_private_event' ); 
    $student->add_cap( 'delete_event' ); 
}

答案1

得分: 2

默认情况下,注册自定义文章类型是设置为文章(post),这意味着您需要授予角色编辑文章的权限。如果您想将帖子/博客和新的自定义文章类型分开,您需要将以下行添加到自定义文章类型的注册中:'capability_type' => 'event',

您的代码应该如下所示。

function mepoNET_post_types() {
    register_post_type('event', array(
        'public'            => true,
        'labels'            => array(
            'name'          => 'Events',
            'add_new_item'  => 'Add New Event',
            'edit_item'     => 'Edit Event',
            'all_items'     => 'All Events',
            'singular_name' => 'Event'
        ),
        'menu_icon'         => 'dashicons-calendar-alt',
        'supports'          => array('title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments'),
        'show_in_rest'      => true,
        'taxonomies'        => array('category', 'post_tag'),
        'has_archive'       => true,
        'capability_type'   => 'event',
        'capabilities'      => array(
            'edit_events' => true,
            'read_events' => true,
            'delete_events' => true,
            'publish_events' => true,
        ),
        'map_meta_cap'      => true,
    ));
}

add_action('init', 'mepoNET_post_types');

新角色的注册应如下所示:

function mepoNET_roles_activation() {
    add_role(
        'mepo_student',
        'Student',
        array(
            'edit_events' => true,
            'read_events' => true,
            'delete_events' => true,
            'publish_events' => true,
            'read_private_events' => true,
            'delete_events' => true,
            'read' => true,
            'edit_posts' => false,
            'delete_posts' => false,
            'publish_posts' => false,
            'upload_files' => true, // 仅在希望学生能够添加图像/文件时添加此项
        ),
    );
}

仪表板应该如下所示:

如何为自定义角色赋予自定义文章类型的功能。

希望这有所帮助。

所有角色和功能的参考列表:

https://wordpress.org/documentation/article/roles-and-capabilities/

英文:

@onlyoreoo,

By default registering a custom post type is set to post, which means you would have to give the role permission to edit a post. If you want to keep posts/blogs and the new custom post type separate, you have to add this line to the registration of the custom post type 'capability_type' => 'event',

Your code should look like this.

function mepoNET_post_types() {
            register_post_type('event', array(
                'public'            => true,
                'labels'            => array(
                    'name'          => 'Events',
                    'add_new_item'  => 'Add New Event',
                    'edit_item'     => 'Edit Event',
                    'all_items'     => 'All Events',
                    'singular_name' => 'Event'
                ),
                'menu_icon'         =>'dashicons-calendar-alt',
                'supports'          => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' ),
                'show_in_rest'      => true,
                'taxonomies'        => array( 'category', 'post_tag' ),
                'has_archive'       => true,
                'capability_type'   => 'event',
				'capabilities'      => array(
					'edit_events' => true,
					'read_events' => true,
					'delete_events' => true,
					'publish_events' => true,
					),
                'map_meta_cap'      => true,
            ));
}
        
        add_action('init', 'mepoNET_post_types');

The registration for the new role should look like this:

function mepoNET_roles_activaton() {
		add_role(
			'mepo_student',
			'Student',
			array(
				'edit_events' => true,
				'read_events' => true,
				'delete_events' => true,
				'publish_events' => true,
				'read_private_events' => true,
				'delete_events' => true,
				'read' => true,
				'edit_posts' => false,
				'delete_posts' => false,
				'publish_posts' => false,
				'upload_files' => true, // only add this if you want the students to be able to add images/files
			),
		);
	}

Dashboard should look like this:

如何为自定义角色赋予自定义文章类型的功能。

Hope this helps.

Reference list of all Roles and Capabilities:

https://wordpress.org/documentation/article/roles-and-capabilities/

huangapple
  • 本文由 发表于 2023年8月4日 04:39:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76831475.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定