Support » Plugin: User Role Editor » Create capability in code so it can be selected

  • Resolved Mark Krieger

    (@markcanada)


    Hi there

    What code do I have to add to my plugin so that a custom capability can be selected in your plugin to be applied to a role.
    Use case:
    I am displaying an admin submenupage for WooCommerce and right now it requires the capability “manage_woocommerce”
    A client wants to give another role access to my plugin, but not to all of WooCommerce.

    I an only find ode online for custom post type registrations or adding capabilities to roles, but not how to add a capability that can be used later.

    Thanks

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Vladimir Garagulia

    (@shinephp)

    Hi,

    Right, use WordPress roles API (wp-includes/wp-roles.php, wp-includes/wp-role.php) for adding capability to a role. Add your custom capability to the ‘administrator’ role. It will be automatically used/shown by URE in the list of the capabilities – group ‘Custom capabilities’.
    For example:

    
    $roles = wp_roles();
    $role = $roles->role_objects['administrator'];
    $role->add_cap('test_custom_cap');
    

    You may add it to the code linked to your plugin activation.

    Mark Krieger

    (@markcanada)

    Ah. So a capability can’t exist if it’s not attached to a role.
    That explains why I couldn’t find how to do it 🙂

    Thank you very much for the explanation and code sample.

Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.