Description
Filters menu item visibility.
Parameters
$visibility
(boolean)(required) Whether the item is visible in the current menu (true) or not (false).
$item
(object)(optional) The WP Menu item object.
Usage
This example shows how you could use a ternary operator to toggle a menu item’s visibility based on the user’s login status:
add_filter( 'wpmem_menu_item_visibility', function( $visible, $item ) {
// $item->title is the EXACT title from the menu "Navigation Label" input.
// This example is for a menu item with the navigation label of "Login"
if ( 'Login' == $item->title ) {
// Ternary operator "if" user is logged in, value is false, otherwise true.
$visible = ( is_user_logged_in() ) ? false : true;
}
// $item->title is the EXACT title from the menu "Navigation Label" input.
// This example is for a menu item with the navigation label of "Register"
if ( 'Register' == $item->title ) {
// Ternary operator "if" user is NOT logged in, value is true, otherwise false.
$visible = ( ! is_user_logged_in() ) ? true : false;
}
return $visible;
},10,2);This example shows the use of a PHP switch to toggle item visibility:
add_filter( 'wpmem_menu_item_visibility', function( $visible, $item ) {
// Cases for menu items (by title) display status for logged in users.
if ( is_user_logged_in() ) {
switch ( $item->title ) {
case 'Login':
return false;
break;
default:
$visible = true;
break;
}
}
// Cases for menu items (by title) display status for NOT logged in users.
if ( ! is_user_logged_in() ) {
switch ( $item->title ) {
case 'Login':
return false;
break;
default:
$visible = true;
break;
}
}
return $visible;
},10,2);Changelog
- Introduced in 3.3.0
Source
wpmem_menu_item_visibility is located in includes/class-wp-members-menu.php