英文:
Remove specific menu item from WordPress admin menu for a specific role
问题
我需要从管理员菜单中移除Premmerce插件的菜单项(仅对管理员可见,对所有其他角色隐藏)。
我之前在JetPack菜单项上遇到了相同的问题,通过将以下代码添加到functions.php文件中解决了该问题:
function ap_remove_jetpack_page() {
if (class_exists('Jetpack') && !current_user_can('manage_options')) {
remove_menu_page('jetpack');
}
}
add_action('admin_menu', 'ap_remove_jetpack_page', 999);
我想要对Premmerce菜单项执行相同的操作。如何实现?
谢谢!
英文:
I need to remove the Premmerce plugin menu item from the admin menu (leaving it visible only to the admin and hidden for all other roles).
I had the same problem with the JetPack menu item which I fixed by adding this code to the functions.php file:
function ap_remove_jetpack_page( ) {
if ( class_exists( 'Jetpack' ) && !current_user_can( 'manage_options' ) ) {
remove_menu_page( 'jetpack' );
}
}
add_action( 'admin_menu', 'ap_remove_jetpack_page', 999 );
I would like to do the same for the Premmerce menu item. How can I do?
Thankyou
答案1
得分: 2
如果您正在使用主要的“premmerce”插件,那么您可以使用以下代码。
function remove_premmerce_menu_item() {
// 检查Premmerce插件是否已激活
if (is_plugin_active('premmerce/premmerce.php') && !current_user_can('manage_options')) {
remove_menu_page('premmerce');
}
}
add_action('admin_menu', 'remove_premmerce_menu_item', 999);
英文:
If you are using the main premmerce
plugin then you can use following code.
function remove_premmerce_menu_item() {
// Check if Premmerce plugin is active
if ( is_plugin_active( 'premmerce/premmerce.php' ) && ! current_user_can( 'manage_options' ) ) {
remove_menu_page( 'premmerce' );
}
}
add_action( 'admin_menu', 'remove_premmerce_menu_item', 999 );
答案2
得分: 0
current_user_can
已足够。
你需要知道的是slug,可以通过以下步骤完成:
你可以测试其他插件以验证行为是否仍然正确。
来源
- https://wordpress.stackexchange.com/questions/103917/removing-the-main-link-to-jetpack-from-the-menu
- https://wpsimplehacks.com/how-to-remove-wordpress-admin-menu-items/
- https://plugins.svn.wordpress.org/ 插件slug列表
英文:
Actually you don't need to check this statement
class_exists( 'Jetpack' )
current_user_can
is enough.
What you need is to know the slug this can be done by following those steps from the extension search page
You can test with other plugins to verify if the behaviour is still correct.
Sources
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论