Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

My wordpress theme has a custom post-type. Only editors (and higher) should be allowed to use this post type.

I did this with:

function add_capability($role,$cap) {
    $role_obj = get_role($role); // get the the role object
    $role_obj -> add_cap($cap); // add $cap capability to this role object
}

function set_cpt() {
    add_capability('editor', 'edit_cpt');
    add_capability('editor', 'read_cpt');
    add_capability('editor', 'delete_cpt');
    add_capability('editor', 'edit_cpt');
    add_capability('editor', 'edit_cpt');
    add_capability('editor', 'publish_cpt');
    add_capability('editor', 'read_cpt');
}
add_action('init', 'set_cpt');

(I think) in Wordpress 3.1 this was enough - all editors and higher had the right to use the post-type.

Now, in Wordpress 3.2 only editors have the right to use the post-type.

Is there a way to grant privileges to role X and higher or would I have to mention every role?

Thank you!

[edit]

actually I found out that this never worked before. But is there another way to do it?

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

If you use the Members plugin, you can set it up like you want. It also has the possibility to add custom roles (your edit_cpt and so on)

also a lot of plugins also work that way:

<?php if ( current_user_can('manage_options') ) { do_something(); } ?>

share|improve this answer
    
thank you! The Members plugin would surely be a possibility. However I wanted to code it... –  Marcel Jul 25 '11 at 6:23
    
download the Members plugin, see how Justin coded it, copy what you need and paste it in your functions.php file? –  Piet Jul 25 '11 at 7:31
    
In the Members plugin, you can choose the privileges for every role one by one. What I want to do is grant a privilege for ALL roles higher or equal to editor. Not that it would be a big deal to mention all roles one by one in the functions.php, I just want to know if there's a more general solution (for example using capability levels). (But I think levels are deprecated!?) –  Marcel Jul 25 '11 at 8:43
1  
"grant a privilege for ALL roles higher or equal to editor", aren't that only the editor and the administrator Roles? Perhaps you can look at a capability that both editor and administrator have, but the rest not. A lot of plugins also work that way: <?php if ( current_user_can('manage_options') ) { do_something(); } ?> –  Piet Jul 26 '11 at 2:07
    
looks good! Thank you! –  Marcel Jul 26 '11 at 7:46
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.