0

I need to show one item from menu only for users that are logged in and have certain custom capability set.

Is it doable from theme functions.php or it needs a custom plugin?

Note: I already use plugin to show/hide menu items based on user roles, but couldn't find anything for Custom Capabilities (user meta value).

Thanks for your time and help!

1

This plugin provides filter to manage the menu item by meta value:

function custom_menu_item_visibility( $visible, $item ){ 
    if( isset( $item->roles ) ){ 
        $user_id = get_current_user_id(); 
        $user_meta = get_user_meta( $user_id, 'your-meta-key', true ); 
        if ( /* your condition */ ){ 
            $visible = true; 
        } else { 
            $visible = false; 
        } 
    } 
    return $visible; 
} 
add_filter( 'nav_menu_roles_item_visibility', 'custom_menu_item_visibility', 10, 2 ); 
1

To enable menu item based on user role you can try this plugin: https://wordpress.org/plugins/nav-menu-roles/

  • as i wrote in note i already have plugin to display item based on role. I need to display/hide item based on custom capability (user meta value) – toHo May 30 at 12:50
  • 1
    This plugin provides filter to manage the menu item by meta value: function custom_menu_item_visibility( $visible, $item ){ if( isset( $item->roles ) ){ $user_id = get_current_user_id(); $user_meta = get_user_meta( $user_id, 'your-meta-key', true ); if ( /* your condition */ ){ $visible = true; } else { $visible = false; } } return $visible; } add_filter( 'nav_menu_roles_item_visibility', 'custom_menu_item_visibility', 10, 2 ); – Bhupen May 30 at 13:33
  • would you mind editing answer and pasting code there for easier readability? – toHo Jun 4 at 4:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.