因此,大多数插件通常都需要manage_options功能来访问它们的选项页。
一些插件提供了一个钩子,允许您分配一个自定义功能来授予用户对其选项页的访问权限,但许多插件没有。
是否要检查哪个插件选项页正试图被访问(以及由哪个用户访问),然后如果用户应该访问该特定选项页,则添加manage_options功能吗?
我已经搜了很多遍了,还没找到答案.如果这根本不可能,那将是一个可以接受的答案,所以我不再试图做一些无法做到的事情。我试图使用user_has筛选器,但这只返回用户功能,不告诉我他们试图访问什么。
发布于 2023-05-21 14:18:18
当插件没有提供自定义功能时,将“manage_options”功能限制到特定插件的特定用户是相当复杂的。
因此,最好与插件开发人员联系,并询问是否有挂钩或过滤器来更改访问插件选项所需的功能。这将是一个更安全的解决方案。
如果由于某些原因,他们无法为您提供钩子或筛选器,那么您可以做的是使用‘admin_init钩子创建一个自定义函数,它将检查当前页面和用户,如果它们满足您的特定条件,则临时授予他们’manage_options的功能。
例如:
function my_custom_capabilities() {
// Get current user
$current_user = wp_get_current_user();
// Get current page
global $pagenow;
// Check if we're on a specific plugin's options page and if the user is the one specified
if ($pagenow == 'options-general.php' && isset($_GET['page']) && $_GET['page'] == 'your-plugin-slug' && $current_user->ID == 'your-user-ID') {
// Temporarily add the 'manage_options' capability to the user
$current_user->add_cap('manage_options');
} else {
// If we're not on the specific plugin's page or if the user is not the one specified, remove the 'manage_options' capability
$current_user->remove_cap('manage_options');
}
}
add_action('admin_init', 'my_custom_capabilities');将your-plugin-slug替换为您想要提供访问权限的插件的选项页的段塞,并将your-user-ID替换为要赋予manage_options功能的用户的ID。
此脚本仅在指定插件的选项页上向指定用户添加manage_options功能。一旦他们离开该页面导航,功能就会被删除。
小心,这种方法存在潜在的安全风险,因为在该页面上,用户在技术上将具有manage_options功能,因此他们可能会使用它来访问其他设置。请仔细使用它,并在实施之前考虑所有潜在的影响。
https://wordpress.stackexchange.com/questions/416128
复制相似问题