Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Faisalawanisee/90b3a79a6211d3aa252c8a51709b876b to your computer and use it in GitHub Desktop.
Save Faisalawanisee/90b3a79a6211d3aa252c8a51709b876b to your computer and use it in GitHub Desktop.
Add custom capabilities to custom post type
<?php
function charity_post_type_register() {
$labels = array(
'name' => __( 'Charities', 'text-domain' ),
'singular_name' => __( 'Charity', 'text-domain' ),
'add_new' => _x( 'Add New Charity', 'text-domain', 'text-domain' ),
'add_new_item' => __( 'Add New Charity', 'text-domain' ),
'edit_item' => __( 'Edit Charity', 'text-domain' ),
'new_item' => __( 'New Charity', 'text-domain' ),
'view_item' => __( 'View Charity', 'text-domain' ),
'search_items' => __( 'Search Charities', 'text-domain' ),
'not_found' => __( 'No Charities found', 'text-domain' ),
'not_found_in_trash' => __( 'No Charities found in Trash', 'text-domain' ),
'parent_item_colon' => __( 'Parent Charity:', 'text-domain' ),
'menu_name' => __( 'Charities', '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' => false,
'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,
'capability_type' => 'charity',
'capabilities' => array(
'create_posts' => 'do_not_allow',
'publish_posts' => 'publish_charities',
'edit_posts' => 'edit_charities',
'edit_others_posts' => 'edit_others_charities',
'delete_posts' => 'delete_charities',
'delete_others_posts' => 'delete_others_charities',
'read_private_posts' => 'read_private_charities',
'edit_post' => 'edit_charity',
'delete_post' => 'delete_charity',
'read_post' => 'read_charity',
),
//'map_meta_cap' => true,
'supports' => array(
'title',
'editor',
'thumbnail',
),
);
register_post_type( 'slug', $args );
}
add_action( 'init', 'charity_post_type_register' );
add_filter( 'map_meta_cap', 'my_map_meta_cap', 10, 4 );
function my_map_meta_cap( $caps, $cap, $user_id, $args ) {
/* If editing, deleting, or reading a movie, get the post and post type object. */
if ( 'edit_charity' == $cap || 'delete_charity' == $cap || 'read_charity' == $cap ) {
$post = get_post( $args[0] );
$post_type = get_post_type_object( $post->post_type );
/* Set an empty array for the caps. */
$caps = array();
}
/* If editing a movie, assign the required capability. */
if ( 'edit_charity' == $cap ) {
if ( $user_id == $post->post_author )
$caps[] = $post_type->cap->edit_posts;
else
$caps[] = $post_type->cap->edit_others_posts;
}
/* If deleting a movie, assign the required capability. */
elseif ( 'delete_charity' == $cap ) {
if ( $user_id == $post->post_author )
$caps[] = $post_type->cap->delete_posts;
else
$caps[] = $post_type->cap->delete_others_posts;
}
/* If reading a private movie, assign the required capability. */
elseif ( 'read_charity' == $cap ) {
if ( 'private' != $post->post_status )
$caps[] = 'read';
elseif ( $user_id == $post->post_author )
$caps[] = 'read';
else
$caps[] = $post_type->cap->read_private_posts;
}
/* Return the capabilities required by the user. */
return $caps;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment