Ask your WordPress questions! Pay money and get answers fast! (more info)

Access to CPT for Subscribers WordPress

  • SOLVED

<strong>Task:</strong> Subscribers must have access to CPT for making a posts and sending that for review.

Capabilities for Subscribers: edit_post, delete_post, read_post

Originaly, I have working code for all user levels except Subscribers.


function my_item_register() {

// P O S T T Y P E

$labels = array(
'name' => __('Projects','x'),
'singular_name' => __('Project','x'),
'add_new' => __('Add New','x'),
'add_new_item' => __('Add New Project','x'),
'edit_item' => __('Edit Project','x'),
'new_item' => __('New Project','x'),
'view_item' => __('View Project','x'),
'search_items' => __('Search Project','x'),
'not_found' => __('No projects found','x'),
'not_found_in_trash' => __('No projects found in Trash','x'),
'parent_item_colon' => ''
);

$args = array(
'labels' => $labels,
'description' => __('Projects','x'),
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'capability_type' => 'post',
'menu_position' => 5,
'hierarchical' => false,
'rewrite' => array('slug' => 'portfolio', 'with_front' => true),
'query_var' => true,
'has_archive' => true,
'supports' => array('title', 'editor', 'thumbnail', 'comments', 'author', 'page-attributes')
);

register_post_type( 'my_item' , $args );

}

add_action('init', 'my_item_register');


-

at this moment <strong>capability_type => 'post'</strong> and, I guess, that must be unique e.g. <strong>my_item</strong>.

I've tried to set


'capability_type' => 'my_item',
'capabilities' => $capabilities,


and


$capabilities = array(
'edit_posts' => 'edit_posts_my_item',
'edit_others_posts' => 'edit_others_my_item',
'read_private_posts' => 'read_private_my_item',
'publish_posts' => 'publish_my_item',
'edit_post' => 'edit_my_item',
'delete_post' => 'delete_my_item',
'read_post' => 'read_my_item'
);


and share capabilities for Subscriber role


function allow_subscriber_my_items() {
$role = get_role('subscriber');
$arr = array(
'edit_my_item',
'delete_my_item',
'read_my_item'
);
foreach($arr as $key)
$role->add_cap($key);
}
if ($user->roles[0] == 'subscriber')
add_action('init', 'allow_subscriber_my_items');


Unfortunately, that do not works for me.

Answers (2)

2012-10-21

Manoj Raj answers:

Try the following code


function add_subscriber_caps() {
// Get the Subscriber role
$role = get_role( 'subscriber' );

$role->add_cap( 'edit_posts_my_item' );
$role->add_cap( 'edit_my_item' );
$role->add_cap( 'delete_my_item' );
$role->add_cap( 'read_my_item' );
}
add_action( 'admin_init', 'add_subscriber_caps');


Igor comments:

Oh, right :)

<strong>admin_init</strong> must be used insyead of <strong>init</strong>

Thank you!

2012-10-21

Arnav Joy answers:

see this plugin

http://wordpress.org/extend/plugins/advanced-access-manager/


Igor comments:

I need to make that without plugin.