插件创建-如何添加用户权限?

时间:2013-07-26 作者:Manticore

我写了一个wordpress插件,我只想授予特定角色或用户访问它的权限。有没有办法做到这一点?

2 个回复
最合适的回答,由SO网友:gmazzap 整理而成

这里介绍如何为插件注册特定的角色,并向选定的核心角色添加特定于插件的功能

function my_plugin_install() {
   // add a new role for plugin with some capabilities
   add_role(\'my_plugin_role\', \'My Plugin Role\', array(
     \'manage_my_plugin\' => true, // plugin specific capability
     \'read\' => true // core capability
   ));
   // add plugin capabilities for some standard roles
   $roles = array(\'administrator\', \'editor\', \'author\');
   $roles_obj = new WP_Roles();
   foreach ($roles as $role_name) {
      $roles_obj->add_cap($role_name, \'manage_my_plugin\' ); 
   }
}


function my_plugin_unistall() {
    remove_role( \'my_plugin_role\' );
    $roles = array(\'administrator\', \'editor\', \'author\');
    $roles_obj = new WP_Roles();
    foreach ($roles as $role_name) {
      $roles_obj->remove_cap($role_name, \'manage_my_plugin\' ); 
    }
}
在插件文件中,输入:

register_activation_hook(__FILE__, \'my_plugin_install\' );
register_uninstall_hook(__FILE__, \'my_plugin_unistall\');
要签入当前登录用户是否具有插件功能,请使用:

if ( current_user_can(\'manage_my_plugin\') ) {
  // do something
}
文档:

结束

相关推荐