1

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?

3

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(); } ?>

  • 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? – user2015 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
  • 2
    "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(); } ?> – user2015 Jul 26 '11 at 2:07
  • glad that eventually it works for you :) – user2015 Jul 26 '11 at 8:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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