Add Custom User Capabilities Before or After the Custom User Role has Been Added?

There’s a reason why add_role() has the $capabilities as 3rd parameter.

First some insights on what happens, when you use the function.

  1. It calls WP_Roles->add_role() – the class method
  2. The method then does a check if the role already exists. If yes, it aborts.
  3. The next step is, that it adds the role to WP_Roles->roles[] array with the display_name (2nd arg) to the $wpdb->prefix . 'user_roles' option table entry (an array).

Conclusion

So if you don’t add the capabilities right when creating the role, then you save a role with empty capabilities.

A note about the Codex

NEVER EVER trust the codex. There simply is no function called add_cap(). There’re only class methods inside WP_Roles, WP_Role and WP_User that are named like this. (Will update the Codex entry if I find some time.)

Leave a Comment