如上所述,删除添加的功能更容易。下面是一个简单的实现示例:
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\'));