Created
March 13, 2024 10:46
-
-
Save vishalkakadiya/64c66f52e2f98ee28ec84044d3afa6cb to your computer and use it in GitHub Desktop.
Add User Role based on the CPT
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Registers the `news` post type. | |
*/ | |
function news_cpt(): void { | |
$labels = [ | |
'name' => __( 'News', 'text-domain' ), | |
'singular_name' => __( 'News', 'text-domain' ), | |
'all_items' => __( 'All News', 'text-domain' ), | |
'archives' => __( 'News Archives', 'text-domain' ), | |
'attributes' => __( 'News Attributes', 'text-domain' ), | |
'insert_into_item' => __( 'Insert into News', 'text-domain' ), | |
'uploaded_to_this_item' => __( 'Uploaded to this News', 'text-domain' ), | |
'featured_image' => _x( 'Featured Image', 'News', 'text-domain' ), | |
'set_featured_image' => _x( 'Set featured image', 'News', 'text-domain' ), | |
'remove_featured_image' => _x( 'Remove featured image', 'News', 'text-domain' ), | |
'use_featured_image' => _x( 'Use as featured image', 'News', 'text-domain' ), | |
'filter_items_list' => __( 'Filter News list', 'text-domain' ), | |
'items_list_navigation' => __( 'News list navigation', 'text-domain' ), | |
'items_list' => __( 'News list', 'text-domain' ), | |
'new_item' => __( 'New News', 'text-domain' ), | |
'add_new' => __( 'Add New', 'text-domain' ), | |
'add_new_item' => __( 'Add New News', 'text-domain' ), | |
'edit_item' => __( 'Edit News', 'text-domain' ), | |
'view_item' => __( 'View News', 'text-domain' ), | |
'view_items' => __( 'View News', 'text-domain' ), | |
'search_items' => __( 'Search News', 'text-domain' ), | |
'not_found' => __( 'No News found', 'text-domain' ), | |
'not_found_in_trash' => __( 'No News found in trash', 'text-domain' ), | |
'parent_item_colon' => __( 'Parent News:', 'text-domain' ), | |
'menu_name' => __( 'News', 'text-domain' ), | |
]; | |
$args = [ | |
'labels' => $labels, | |
'public' => true, | |
'hierarchical' => false, | |
'show_ui' => true, | |
'show_in_nav_menus' => true, | |
'supports' => [ 'title', 'editor', 'excerpt', 'author', 'thumbnail' ], | |
'has_archive' => false, | |
'query_var' => true, | |
'menu_position' => null, | |
'menu_icon' => 'dashicons-admin-post', | |
'show_in_rest' => true, | |
'capability_type' => 'news', | |
'map_meta_cap' => true, // Important for mapping custom capabilities | |
'capabilities' => [ | |
'edit_post' => 'edit_news', | |
'read_post' => 'read_news', | |
'delete_post' => 'delete_news', | |
'edit_posts' => 'edit_newss', | |
'edit_others_posts' => 'edit_others_newss', | |
'publish_posts' => 'publish_newss', | |
'read_private_posts' => 'read_private_newss', | |
], | |
]; | |
register_post_type( 'news', $args ); | |
} | |
add_action( 'init', 'news_cpt' ); | |
/** | |
* Add news editor role. | |
*/ | |
function add_news_editor_role() { | |
// Add a new role with 'read' capability. | |
add_role('news_editor', 'News Editor', array('read' => true)); | |
// Get the roles object | |
$roles = array( 'administrator', 'news_editor' ); | |
// Capabilities to add | |
$caps = array( | |
'edit_news', | |
'read_news', | |
'delete_news', | |
'edit_newss', | |
'edit_others_newss', | |
'publish_newss', | |
'read_private_newss', | |
); | |
foreach ($roles as $the_role) { | |
$role = get_role($the_role); | |
foreach ($caps as $cap) { | |
// Add new capabilities to Administrator and News Editor roles | |
$role->add_cap($cap); | |
} | |
} | |
} | |
add_action('init', 'add_news_editor_role', 11 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment