有没有办法恢复核心的可湿性粉剂能力?

时间:2013-09-02 作者:TobyG

我创建了一个插件,添加了新功能&;将它们分配给WP和;核心WP角色。

我想在插件停用时删除这些功能,但我需要检查我没有删除核心WP功能。有没有办法获得一个只包含WP核心功能的阵列,而不是对列表进行硬编码(这不是未来的证明)?

1 个回复
SO网友:gmazzap

如上所述,删除添加的功能更容易。下面是一个简单的实现示例:

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\'));

结束

相关推荐

PHP致命错误:无法为wp-includes/capabilities.php中的非对象调用重载函数

我在apache日志中遇到了太多以下错误。PHP Fatal error: Cannot call overloaded function for non-object in wp-includes/capabilities.php on line 1187这是函数current\\u user\\u can($capability)的内部,第1187行如下所示:$current_user = wp_get_current_user(); 我不知道问题出在哪里?