Menu item visibility by Groups

Is there a way (or can you create one an addon for this) to allow visibility restriction of non-page menu items? So currently I have the Groups plugin setup, and on those pages I have view capabilities tied to groups, and the by-product is that the visibility for the menu items linking to those page is restricted to those who are logged in and are in a specific group. That works terrifically. But when I add other types of menu items that aren’t based on a page (direct links for instance), there is no way that I can see to restrict visibility.

What I’m trying to do is have a logout link in my Members Area menu that only shows up when logged in. Currently the only way that I can make this work is to link to a “Logout page” (setup with view capabilities based on groups) that then presents a logout link on that page again … which is dumb. Can you help?

Thanks,
Josh


Comments

One response to “Menu item visibility by Groups”

  1. Hi Josh,

    You can use the filter below to exclude the link named “Logout” from the menu assigned to Main menu.

    function gt_exclude_menu_items( $items, $menu, $args ) {
    // Get the menu items
    foreach ( $items as $key => $item ) {
    if ( $item->post_title == 'Logout' ) {
    if ( !is_user_logged_in() ) {
    unset( $items[$key] );
    }
    }
    }
    write_log($items);
    return $items;
    }

    add_filter( 'wp_get_nav_menu_items', 'gt_exclude_menu_items', 10, 3 );

    Kind regards,
    George

Share