从WordPress管理菜单中删除特定角色的特定菜单项。

huangapple go评论46阅读模式
英文:

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,可以通过以下步骤完成:

  1. 点击更多详情
    从WordPress管理菜单中删除特定角色的特定菜单项。
  2. 点击WordPress.org扩展页面
    从WordPress管理菜单中删除特定角色的特定菜单项。
  3. slug在URL中
    从WordPress管理菜单中删除特定角色的特定菜单项。

你可以测试其他插件以验证行为是否仍然正确。

来源

英文:

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

  1. Click on more details
    从WordPress管理菜单中删除特定角色的特定菜单项。
  2. Click on WordPress.org extension page
    从WordPress管理菜单中删除特定角色的特定菜单项。
  3. The slug is in the URL
    从WordPress管理菜单中删除特定角色的特定菜单项。

You can test with other plugins to verify if the behaviour is still correct.

Sources

huangapple
  • 本文由 发表于 2023年7月20日 18:36:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76729027.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定