nav_menu_roles_item_visibility
Filters whether a navigation menu item is visible based on user roles before it is displayed.
add_filter( 'nav_menu_roles_item_visibility', $callback, 10, 2 );
Description
Filters whether a menu item should be visible to the current user. Allows modification of visibility based on user roles or custom logic, useful for conditionally hiding or showing navigation elements. The `$visible` parameter determines visibility, and `$item` provides details about the menu item itself.
Usage
add_filter( 'nav_menu_roles_item_visibility', 'your_function_name', 10, 2 );
Parameters
-
$visible(mixed) - This parameter determines whether a menu item should be visible to the current user.
-
$item(mixed) - This parameter indicates whether the current menu item should be visible to the current user.
Return Value
The filtered value.
Examples
add_filter( 'nav_menu_roles_item_visibility', 'my_custom_menu_item_visibility', 10, 2 );
/**
* Example function to modify menu item visibility based on custom criteria.
*
* This function checks if a menu item should be visible based on user roles
* and an additional custom capability.
*
* @param bool $visible The current visibility status of the menu item.
* @param object $item The menu item object.
* @return bool The modified visibility status of the menu item.
*/
function my_custom_menu_item_visibility( $visible, $item ) {
// If the item is already set to be not visible, don't override it.
if ( ! $visible ) {
return false;
}
// Check if the menu item has a custom meta field for 'custom_capability'.
// This assumes you have a way to set this meta data for menu items,
// perhaps through a custom plugin or theme functionality.
$custom_capability_required = get_post_meta( $item->ID, '_custom_capability', true );
// If a custom capability is defined for this menu item.
if ( ! empty( $custom_capability_required ) ) {
// Check if the current user has the specified custom capability.
if ( ! current_user_can( $custom_capability_required ) ) {
// If the user doesn't have the capability, make the item not visible.
$visible = false;
}
}
// Return the final visibility status.
return $visible;
}
Placement
This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.
Source Code
src/classes/menu-item-visibility.php:288
public static function exclude_menu_items( $items ) {
if ( is_admin() ) {
return $items;
}
$hide_children_of = array();
// Iterate over the items to search and destroy
foreach ( $items as $key => $item ) {
$visible = true;
// hide any item that is the child of a hidden item
if ( in_array( $item->menu_item_parent, $hide_children_of ) ) {
$visible = false;
$hide_children_of[] = $item->ID; // for nested menus
}
// check any item that has NMR roles set
if ( $visible && isset( $item->roles ) ) {
// check all logged in, all logged out, or role
switch ( $item->roles ) {
case 'in' :
$visible = is_user_logged_in() ? true : false;
break;
case 'out' :
$visible = ! is_user_logged_in() ? true : false;
break;
default:
$visible = false;
if ( is_array( $item->roles ) && ! empty( $item->roles ) ) {
foreach ( $item->roles as $role ) {
if ( current_user_can( $role ) ) {
$visible = true;
}
}
}
break;
}
}
// add filter to work with plugins that don't use traditional roles
$visible = apply_filters( 'nav_menu_roles_item_visibility', $visible, $item );
// unset non-visible item
if ( ! $visible ) {
$hide_children_of[] = $item->ID; // store ID of item
unset( $items[ $key ] );
}
}
return $items;
}