0

I've created a plugin that adds new capabilities & assigns them to both WP & core WP roles.

I would like to remove these capabilities when the plugin is deactivated, but I need to check that I'm not removing a core WP capability. Is there a way to get an array containing just the WP core capabilities rather than hard coding the list (which would not be future-proof)?

  • 1
    Is not easier remove the added capabilities? – gmazzap Sep 2 '13 at 14:25
  • I suppose, but my plugin also adds custom roles & then assigns them standard capabilities, so I'll need to keep those assignments separate from the custom capabilities to be able to tell which is which later on. Was just curious to see if there was a better way. – TobyG Sep 4 '13 at 10:37
  • I admit I don't understand the problem with custom roles & standard capabilities: If your problem is remove caps on deactiovation don't you remove the custom roles at all? However as far I know, there is no way to retrieve all standard capabilities. You can retrieve the admin capabilities they are all standard + any cap addae by others plugins... – gmazzap Sep 4 '13 at 15:48
  • Well I think if there's no way to retrieve the standard capabilities, that answers my question. Thanks @g-m – TobyG Sep 5 '13 at 16:48
0

As said in comment above is more easy remove the added capabilities. Below, as example, a simple implementation:

class MyAwesomePlugin {

  static $capabilities = array('a_cap'=> true, 'another_one'=> true, 'third_cap'=> true);

  static $custom_roles = array('one_role' => 'One Role', 'second_role' => 'Second Role');

  static $core_roles = array('administrator', 'editor', 'author');

  static function install() {
    // add custom capabilities to core roles
    $roles_obj = new WP_Roles();
    foreach (self::$core_roles as $role_name) {
      foreach ( self::$capabilities as $cap => $bool )
        if ($bool) $roles_obj->add_cap($role_name, $cap );
    }
    // add custom roles with custom capabilities
    foreach ( self::$custom_roles as $custom_role => $label)
      add_role( $custom_role, $label, self::$capabilities);
  }

  static function uninstall() {
    // remove custom roles
    foreach ( self::$custom_roles as $custom_role => $label)
      remove_role( $custom_role );
    // remove custom capabilitie from core roles
    $roles_obj = new WP_Roles();
    foreach (self::$core_roles as $role_name) {
      foreach ( self::$capabilities as $cap => $bool )
        $roles_obj->remove_cap($role_name, $cap );
    }
  }

}

register_activation_hook(__FILE__, array('MyAwesomePlugin', 'install') );
register_deactivation_hook(__FILE__, array('MyAwesomePlugin', 'uninstall'));
  • Please see my comment below the question, as there's a slightly more complex situation. – TobyG Sep 4 '13 at 10:38

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.