Created
July 18, 2020 10:57
-
-
Save maheshwaghmare/3b4ee08bbe205f0a98aafb935aea688f to your computer and use it in GitHub Desktop.
Sample Plugin - Create new user role and new post type and allow to create the custom post type with those users who have that specific user role.
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 | |
<?php | |
/** | |
* Plugin name: Sample Plugin | |
*/ | |
/** | |
* User Role Specific Post Type | |
* | |
* @package User Role Specific Post Type | |
*/ | |
if( ! class_exists( 'Sample_Plugin' ) ) : | |
/** | |
* Sample Plugin | |
*/ | |
class Sample_Plugin { | |
/** | |
* Member Variable | |
* | |
* @var instance | |
*/ | |
private static $instance; | |
/** | |
* Initiator | |
* | |
* @since x.x.x | |
*/ | |
public static function get_instance() { | |
if ( ! isset( self::$instance ) ) { | |
self::$instance = new self; | |
} | |
return self::$instance; | |
} | |
/** | |
* Constructor | |
* | |
* @since x.x.x | |
*/ | |
public function __construct() { | |
add_action( 'admin_init', array( $this, 'add_user_role' ), 999 ); | |
add_action( 'init', array( $this, 'register_post_type' ) ); | |
} | |
/** | |
* Add user role | |
* | |
* @return void | |
*/ | |
function add_user_role() { | |
$role = get_role( 'doc_writer' ); | |
if ( ! $role ) { | |
add_role( 'doc_writer', 'Doc Writer' ); | |
$role = get_role( 'doc_writer' ); | |
} | |
$role->add_cap( 'level_0' ); | |
$role->add_cap( 'read' ); | |
// Primitive capabilities used within map_meta_cap(). | |
$role->add_cap( 'read' ); | |
$role->add_cap( 'delete_doc_writer' ); | |
$role->add_cap( 'read_private_doc_writers' ); | |
$role->add_cap( 'delete_private_doc_writer' ); | |
$role->add_cap( 'delete_published_doc_writer' ); | |
$role->add_cap( 'delete_others_doc_writer' ); | |
$role->add_cap( 'edit_private_doc_writer' ); | |
$role->add_cap( 'edit_published_doc_writer' ); | |
// Meta capabilities. | |
$role->add_cap( 'edit_doc_writer' ); | |
$role->add_cap( 'edit_doc_writers' ); | |
$role->add_cap( 'edit_other_doc_writer' ); | |
$role->add_cap( 'read_doc_writer' ); | |
// Primitive capabilities used outside of map_meta_cap(). | |
$role->add_cap( 'edit_others_doc_writer' ); | |
$role->add_cap( 'publish_doc_writer' ); | |
$role->add_cap( 'read_private_doc_writer' ); | |
$role->add_cap( 'upload_files' ); | |
$role->add_cap( 'delete_posts' ); | |
// Enable to delete attachments. | |
$role->add_cap( 'delete_others_posts' ); | |
} | |
function register_post_type() { | |
$labels = array( | |
'name' => __( 'Docs', 'text-domain' ), | |
'singular_name' => __( 'Doc', 'text-domain' ), | |
'add_new' => _x( 'Add New Doc', 'text-domain', 'text-domain' ), | |
'add_new_item' => __( 'Add New Doc', 'text-domain' ), | |
'edit_item' => __( 'Edit Doc', 'text-domain' ), | |
'new_item' => __( 'New Doc', 'text-domain' ), | |
'view_item' => __( 'View Doc', 'text-domain' ), | |
'search_items' => __( 'Search Docs', 'text-domain' ), | |
'not_found' => __( 'No Docs found', 'text-domain' ), | |
'not_found_in_trash' => __( 'No Docs found in Trash', 'text-domain' ), | |
'parent_item_colon' => __( 'Parent Doc:', 'text-domain' ), | |
'menu_name' => __( 'Docs', 'text-domain' ), | |
); | |
$args = array( | |
'labels' => $labels, | |
'hierarchical' => false, | |
'description' => 'description', | |
'taxonomies' => array(), | |
'public' => true, | |
'show_ui' => true, | |
'show_in_menu' => true, | |
'show_in_admin_bar' => true, | |
'menu_position' => null, | |
'menu_icon' => null, | |
'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_doc_writer', | |
'edit_posts' => 'edit_doc_writers', | |
'edit_others_posts' => 'edit_other_doc_writer', | |
'publish_posts' => 'publish_doc_writer', | |
'read_post' => 'read_doc_writer', | |
'read_private_posts' => 'read_private_doc_writers', | |
'delete_post' => 'delete_doc_writer', | |
), | |
'supports' => array( | |
'title', | |
'editor', | |
), | |
); | |
register_post_type( 'doc', $args ); | |
} | |
} | |
/** | |
* Kicking this off by calling 'get_instance()' method | |
*/ | |
Sample_Plugin::get_instance(); | |
endif; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment