最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

admin menu - add_menu_page not show link for custom role

programmeradmin0浏览0评论

I write a custom plugin, with one link for administration purpose. This is my code:

add_action('admin_menu', 'tb_instructor_menu');
add_action( 'init', 'aggiungi_ruolo', 11 );
function aggiungi_ruolo() 
{
$role = get_role( 'wdm_instructor' );
$role->add_cap( 'view_webinar', true );
}
function tb_instructor_menu()
{
add_menu_page('My code', 'My code', 'view_webinar', 'my-code', null, 'dashicons-businessman' , 61);
}

If I am logged in as Admin, I see the link.

If I'm logged in as "wdm_instructor", I don't see the link.

I have already verified that the "wdm_instructor" role has the "view_webinar" capability flagged.

I write a custom plugin, with one link for administration purpose. This is my code:

add_action('admin_menu', 'tb_instructor_menu');
add_action( 'init', 'aggiungi_ruolo', 11 );
function aggiungi_ruolo() 
{
$role = get_role( 'wdm_instructor' );
$role->add_cap( 'view_webinar', true );
}
function tb_instructor_menu()
{
add_menu_page('My code', 'My code', 'view_webinar', 'my-code', null, 'dashicons-businessman' , 61);
}

If I am logged in as Admin, I see the link.

If I'm logged in as "wdm_instructor", I don't see the link.

I have already verified that the "wdm_instructor" role has the "view_webinar" capability flagged.

Share Improve this question asked May 29, 2020 at 12:32 InfocurciInfocurci 1 5
  • How did you confirm that role has the view_webinar capability? Keep in mind that when you call add_cap it makes changes to the database, your code as it is will make an unnecessary database write on every single request/page/ajax/RSS/REST etc This might be related to your problem – Tom J Nowell Commented May 29, 2020 at 12:41
  • Whe I'm logged as wdm_instructor, with wp_get_current_user() I see that my role is "wdm_instructor". With get_role( 'wdm_instructor' )->capabilities; I see that I have [view_webinar] => 1. – Infocurci Commented May 29, 2020 at 13:52
  • and the page works if you visit it directly? It just doesn't show in the admin menu for that specific role? Is your administrator an administrator or is it a super admin on a multisite? Why is your callback for rendering the page set to null? – Tom J Nowell Commented May 29, 2020 at 14:03
  • I had disabled the callback to make sure that wasn't the problem. Even with the callback, I don't see the link in the menu when I'm logged in as wdm_instructor. I only see the link if I'm a wordpress administrator. If I copy / paste the link into a browser where I am logged in as "wdm_instructor", the page works correctly. Only the link in the menu is missing. – Infocurci Commented Jun 1, 2020 at 6:40
  • So the callback works, it's just that the menu does not appear in the sidebar of WP Admin. I still think that by adding the capability to the role each, and, every, single, page, request, you're actually causing problems. Try unhooking aggiungi_ruolo, you should not be adding the capability on every single request, it should only happen once – Tom J Nowell Commented Jun 1, 2020 at 8:54
Add a comment  | 

1 Answer 1

Reset to default 0

I understand the problem. My customer had purchased a paid plugin that, on the action "admin_menu", scrolled through the entire global variable $menu and eliminated all the items that were not allowed by their plugin

unset( $menu[ $key ] );

发布评论

评论列表(0)

  1. 暂无评论