1

I need to add custom capabilities to user roles, something along the lines of:

$role->add_cap('xtc5000'); // 5000 can be different

I need to use the 5000 value in another plugin for some specific functionality. I need to extract the capability starting with xtc and then use the number (which can be different).

Pseudo-code:

foreach ($caps as $cap) {
    if (stripos($cap, 'xtc') !== false) {
        $number = (int) str_replace('xtc', '', $cap);
    }
}

// Use $number for some other functionality

Can I add custom named capabilities or am I restricted to standard WordPress ones?

The reason I want to this is to use WordPress' tables instead of creating separate options or a separate table. It would be one extra capability per user role.

As an example for my request, I need all authors to be able to post 1000 articles, all editors to be able to post 2000 and all contributors 3000. This is just an example.

  • Could you elaborate on what you want to use the numbers for? It sounds like you'd be better served having user meta, with a meta key of xtc and the value being whatever number you need. – Jacob Peattie Jul 19 '18 at 14:12
  • I need all authors to be able to post 1000 articles and all editors to be able to post 2000 and all contributors 3000. This is just an example. – Ciprian Jul 19 '18 at 14:21
  • I think a better approach would be to have a separate setting/option that mapped roles to article limits. Like ['author' => 1000, 'editor' => 2000]. Then just get the current user role and look up its value in that option whenever you need that information. Storing data in a role name seems like an inappropriate use of roles to me. – Jacob Peattie Jul 19 '18 at 14:26
  • @JacobPeattie I know it does, that's why I'm looking for a more elegant solution. I wanted to avoid all options, but this was my initial plan, followed by role metas/capabilities. – Ciprian Jul 19 '18 at 14:30
  • Why do you want to avoid setting extra options? – Jacob Peattie Jul 19 '18 at 14:31

Your Answer

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

Browse other questions tagged or ask your own question.