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.

I have a situation where I need users to be flagged as "Instructors" on a site. That role should basically be whatever the user's current role is with the added capability to see comments from students on a custom post type called "Questions".

I'm struggling with how to implement this conceptually. Should I just add a new capability and have it assigned by itself, or should I add it and assign it to a new role as well?

[edit]From kaiser's resources, it appears I should just add the custom capability and assign it to users on an individual basis, perhaps from my own plugin's options/settings admin page, as opposed to creating a specific role[/edit]

What happens if I change the user's role to something else (say from Admin to Editor) but still want them to have that capability? Is this a fairly easy thing to program in a custom plugin (which is where I'm doing all of this from), or is this going to be a monumental undertaking?

[edit]So, I guess this is now my main question. What is the underlying logic when switching user roles from one to another? Do custom capabilities that were assigned carry over when switching user roles, or does the new role assignment overwrite anything else?[/edit]

share|improve this question
1  
Please read into map meta capabilities (related on topic post). And into Users and Roles in general. –  kaiser Feb 24 at 18:26
    
Thanks for the resources kaiser. One question that still remains after looking at those, for me, is if I add a capability and assign it to a user, if I change their role from one of the system-defined ones to another does it overwrite that additional custom capability, or just the set of system-defined ones? –  John Feb 24 at 18:52
    
John, please first update your question (file an edit) with your newly gained knowledge :) –  kaiser Feb 24 at 18:56
    
Done. Thanks much! –  John Feb 24 at 19:04

1 Answer 1

up vote 1 down vote accepted

WordPress capability system roughly works like the following:

  • Capabilities can be attached to users or roles
  • Roles can be attached to users
  • Roles are a flat system, so there is nothing overruling something else
  • Role names are capabilities as well

Keep in mind that role names are capabilities as well, but you shouldn't check against them. The same goes for the deprecated level-0 until level-9 caps.

In theory WordPress supports multiple roles per user - something that is extremely convenient as you can add and remove Capability packages summed under role names. It just has no UI and isn't built in. Oh, and there's (afaik) no plugin that delivers that.

So: yes assigning capabilities users is OK. But keep in mind that this can quickly can get a maintenance nightmare. In most cases it's better (not perfect) to just grab a role, copy its capabilities over to your new role and add the new role to a user. When you then want to change something, just exchange to role for all users that need to have a new set of caps.

Note: This is my opinion. Others might have a different one when it comes to what is a nightmare and what not :)

share|improve this answer
    
Thanks kaiser, this helps immensely. I know that the "User Role Editor" plugin allows assigning multiple roles. I may end up using that plugin in the end, but don't really like the idea of having a custom plugin with some core functionality that is dependent on another third-party plugin, so I want to explore how much work it might take to just bake it in to my own. Knowing that role names are essentially capabilities helps immensely. Thanks. –  John Feb 24 at 20:34
    
@John Role names are capabilities as well. But they're also saved in the database and summon the capabilities. –  kaiser Feb 24 at 21:58

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.