这里介绍如何为插件注册特定的角色,并向选定的核心角色添加特定于插件的功能
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
}
文档: