Sample Custom Role Code Shown in Readme
-
HI Kathy,
Awesome plugin (Helga must be proud)!
I used your sample code to add some custom roles and found that the roles appeared in the “Access Role” section of each menu item, but the menu items were appearing when they shouldn’t.
It turned out that the key is being stored in the postmeta table rather than the value.
_nav_menu_role a:1:{i:0;s:1:"0";}
Instead of:
_nav_menu_role a:1:{i:0;s:8:"new-role";}
Just want to let you know so that you can update the readme to show that you need to enter a key value in the roles filter function and then check against that key value in the visibility filter function.
function kia_new_roles( $roles ){ $roles['new-role-key'] = 'new-role'; return $roles; } add_filter( 'nav_menu_roles', 'kia_new_roles' );
function kia_item_visibility( $visible, $item ){ if( isset( $item->roles ) && is_array( $item->roles ) && in_array( 'new-role-key', $item->roles ) ){ /* if ( // your own custom check on the current user versus 'new-role' status ){ $visible = true; } else { $visible = false; } */ } return $visible; } add_filter( 'nav_menu_roles_item_visibility', 'kia_item_visibility', 10, 2 );
Thanks for sharing, it has saved me a bunch of time.
Be Well,
Joe
- The topic ‘Sample Custom Role Code Shown in Readme’ is closed to new replies.