1 | <?php |
---|
2 | |
---|
3 | /* |
---|
4 | Plugin Name: CPT Example |
---|
5 | Plugin URI: http://wpsmith.net |
---|
6 | Description: Demonstrates WP Core Issue. |
---|
7 | Author: Travis Smith |
---|
8 | Version: 0.0.1 |
---|
9 | Author URI: http://wpsmith.net |
---|
10 | License: GPL3 |
---|
11 | Text Domain: text_domain |
---|
12 | */ |
---|
13 | |
---|
14 | namespace WPS; |
---|
15 | |
---|
16 | add_action( 'init', __NAMESPACE__ . '\custom_post_type', 0 ); |
---|
17 | // Register Custom Post Type |
---|
18 | function custom_post_type() { |
---|
19 | |
---|
20 | $labels = array( |
---|
21 | 'name' => _x( 'Post Types', 'Post Type General Name', 'text_domain' ), |
---|
22 | 'singular_name' => _x( 'Post Type', 'Post Type Singular Name', 'text_domain' ), |
---|
23 | 'menu_name' => __( 'Post Types', 'text_domain' ), |
---|
24 | 'name_admin_bar' => __( 'Post Type', 'text_domain' ), |
---|
25 | 'archives' => __( 'Item Archives', 'text_domain' ), |
---|
26 | 'attributes' => __( 'Item Attributes', 'text_domain' ), |
---|
27 | 'parent_item_colon' => __( 'Parent Item:', 'text_domain' ), |
---|
28 | 'all_items' => __( 'All Items', 'text_domain' ), |
---|
29 | 'add_new_item' => __( 'Add New Item', 'text_domain' ), |
---|
30 | 'add_new' => __( 'Add New', 'text_domain' ), |
---|
31 | 'new_item' => __( 'New Item', 'text_domain' ), |
---|
32 | 'edit_item' => __( 'Edit Item', 'text_domain' ), |
---|
33 | 'update_item' => __( 'Update Item', 'text_domain' ), |
---|
34 | 'view_item' => __( 'View Item', 'text_domain' ), |
---|
35 | 'view_items' => __( 'View Items', 'text_domain' ), |
---|
36 | 'search_items' => __( 'Search Item', 'text_domain' ), |
---|
37 | 'not_found' => __( 'Not found', 'text_domain' ), |
---|
38 | 'not_found_in_trash' => __( 'Not found in Trash', 'text_domain' ), |
---|
39 | 'featured_image' => __( 'Featured Image', 'text_domain' ), |
---|
40 | 'set_featured_image' => __( 'Set featured image', 'text_domain' ), |
---|
41 | 'remove_featured_image' => __( 'Remove featured image', 'text_domain' ), |
---|
42 | 'use_featured_image' => __( 'Use as featured image', 'text_domain' ), |
---|
43 | 'insert_into_item' => __( 'Insert into item', 'text_domain' ), |
---|
44 | 'uploaded_to_this_item' => __( 'Uploaded to this item', 'text_domain' ), |
---|
45 | 'items_list' => __( 'Items list', 'text_domain' ), |
---|
46 | 'items_list_navigation' => __( 'Items list navigation', 'text_domain' ), |
---|
47 | 'filter_items_list' => __( 'Filter items list', 'text_domain' ), |
---|
48 | ); |
---|
49 | $args = array( |
---|
50 | 'label' => __( 'Post Type', 'text_domain' ), |
---|
51 | 'description' => __( 'Post Type Description', 'text_domain' ), |
---|
52 | 'labels' => $labels, |
---|
53 | 'supports' => false, |
---|
54 | 'taxonomies' => array( 'category', 'post_tag' ), |
---|
55 | 'hierarchical' => false, |
---|
56 | 'public' => true, |
---|
57 | 'show_ui' => true, |
---|
58 | 'show_in_menu' => true, |
---|
59 | 'menu_position' => 6.9, |
---|
60 | 'show_in_admin_bar' => true, |
---|
61 | 'show_in_nav_menus' => true, |
---|
62 | 'can_export' => true, |
---|
63 | 'has_archive' => true, |
---|
64 | 'exclude_from_search' => false, |
---|
65 | 'publicly_queryable' => true, |
---|
66 | 'capability_type' => 'cpt', |
---|
67 | 'map_meta_cap' => true, |
---|
68 | 'capabilities' => array( |
---|
69 | // Meta capabilities. |
---|
70 | 'edit_post' => 'edit_cpt', |
---|
71 | 'read_post' => 'read_cpt', |
---|
72 | 'delete_post' => 'delete_cpt', |
---|
73 | |
---|
74 | // Primitive capabilities used within map_meta_cap(). |
---|
75 | 'create_posts' => 'create_cpt', |
---|
76 | |
---|
77 | // Primitive capabilities used outside of map_meta_cap(). |
---|
78 | 'edit_posts' => 'edit_cpts', |
---|
79 | 'edit_others_posts' => 'edit_others_cpts', |
---|
80 | 'publish_posts' => 'publish_cpts', |
---|
81 | 'read_private_posts' => 'read_private_cpts', |
---|
82 | |
---|
83 | // Post type. |
---|
84 | 'read' => 'read', |
---|
85 | 'delete_posts' => 'delete_cpts', |
---|
86 | 'delete_private_posts' => 'delete_private_cpts', |
---|
87 | 'delete_published_posts' => 'delete_published_cpts', |
---|
88 | 'delete_others_posts' => 'delete_others_cpts', |
---|
89 | 'edit_private_posts' => 'edit_private_cpts', |
---|
90 | 'edit_published_posts' => 'edit_published_cpts', |
---|
91 | ) |
---|
92 | ); |
---|
93 | register_post_type( 'cpt', $args ); |
---|
94 | |
---|
95 | } |
---|
96 | |
---|
97 | register_deactivation_hook( __FILE__, __NAMESPACE__ . '\delete_role' ); |
---|
98 | function delete_role() { |
---|
99 | remove_role( 'cpt' ); |
---|
100 | // $role = get_role( 'administrator'); |
---|
101 | // $role->remove_cap('edit_cpt'); |
---|
102 | // $role->remove_cap('read_cpt'); |
---|
103 | // $role->remove_cap('delete_cpt'); |
---|
104 | // $role->remove_cap('create_cpt'); |
---|
105 | // $role->remove_cap('edit_cpts'); |
---|
106 | // $role->remove_cap('edit_others_cpts'); |
---|
107 | // $role->remove_cap('publish_cpts'); |
---|
108 | // $role->remove_cap('read_private_cpts'); |
---|
109 | // $role->remove_cap('edit_published_cpts'); |
---|
110 | // $role->remove_cap('delete_cpts'); |
---|
111 | // $role->remove_cap('delete_private_cpts'); |
---|
112 | // $role->remove_cap('delete_published_cpts'); |
---|
113 | // $role->remove_cap('delete_others_cpts'); |
---|
114 | // $role->remove_cap('edit_published_cpts'); |
---|
115 | // $role->remove_cap('edit_private_cpts'); |
---|
116 | // $role->remove_cap('create_cpts'); |
---|
117 | // $role->remove_cap('edit_caps'); |
---|
118 | } |
---|
119 | |
---|
120 | register_activation_hook( __FILE__, __NAMESPACE__ . '\create_role' ); |
---|
121 | function create_role() { |
---|
122 | add_role( 'cpt', 'CPT Editor (cannot create)', array( |
---|
123 | 'read' => true, |
---|
124 | 'edit_cpts' => true, |
---|
125 | 'publish_cpts' => false, |
---|
126 | 'edit_published_cpts' => true, |
---|
127 | 'upload_files' => false, |
---|
128 | 'delete_published_cpts' => false, |
---|
129 | 'create_cpts' => false, |
---|
130 | ) ); |
---|
131 | } |
---|
132 | |
---|
133 | add_action( 'admin_init', __NAMESPACE__ . '\add_caps_to_admin', 0 ); |
---|
134 | function add_caps_to_admin() { |
---|
135 | $role = get_role( 'administrator' ); |
---|
136 | $role->add_cap( 'edit_cpt' ); |
---|
137 | $role->add_cap( 'read_cpt' ); |
---|
138 | $role->add_cap( 'delete_cpt' ); |
---|
139 | $role->add_cap( 'create_cpt' ); |
---|
140 | $role->add_cap( 'edit_cpts' ); |
---|
141 | $role->add_cap( 'edit_others_cpts' ); |
---|
142 | $role->add_cap( 'publish_cpts' ); |
---|
143 | $role->add_cap( 'read_private_cpts' ); |
---|
144 | $role->add_cap( 'edit_published_cpts' ); |
---|
145 | $role->add_cap( 'delete_cpts' ); |
---|
146 | $role->add_cap( 'delete_private_cpts' ); |
---|
147 | $role->add_cap( 'delete_published_cpts' ); |
---|
148 | $role->add_cap( 'delete_others_cpts' ); |
---|
149 | $role->add_cap( 'edit_published_cpts' ); |
---|
150 | $role->add_cap( 'edit_private_cpts' ); |
---|
151 | $role->add_cap( 'create_cpts' ); |
---|
152 | $role->add_cap( 'edit_caps' ); |
---|
153 | } |
---|