Support » Plugins and Hacks » Hacks » Dynamic menu options based on user

Dynamic menu options based on user

  • I am building a multisite network which uses a single set of menus across all child sites, depending on whether or not a user is logged in. What I am attempting to do, is add various menu buttons/options according to which type of user is logged in and who that particular user is.

    Currently, my header loads one of two menus based on a user being logged in. This will be expanded based on user type once I get the other portion solved.

    <?php if (is_user_logged_in()) {
        'theme_location' => 'user-menu',
        'container_class' => 'nav-center',
    } else {
        'theme_location' => 'public-menu',
        'container_class' => 'nav-center',
    } ?>

    The problem with this approach is that the menu is built in the GUI rather than code. I’m assuming I will need a custom walker for this to work, but am not entirely sure how to get this accomplished.

    What I need is for the user-menu to change the items within the menu based on which user is currently logged in. For example, if user john-doe is logged in, there should be menu options such as:

    And if user jane-smith is logged in, the same menu locations should instead point to:

    Would a custom walker allow for this?

Viewing 2 replies - 1 through 2 (of 2 total)
  • I stumbled upon the wp_nav_menu_items filter as I was nosing through the Walker_Nav_Menu and realized that this could be used to add dynamic menu options. It’s not pretty and I’m sure I have done something screwy with the PHP/HTML mix, but it seems to work. So for those who are looking to do something similar, here ya go:

    function new_nav_menu_items($items) {
      global $user_login;
      $dynamic_link = '<li class="home"><a href="' . network_home_url('/') . $user_login . '/link-1/">Link 1 Text</a></li>';
      $items = $dynamic_link . $items;
      return $items;
    add_filter( 'wp_nav_menu_items', 'new_nav_menu_items' );

    Okay, I have more problems for anyone who is capable of assisting.

    The above code will either allow me to append the new menu items on the front or back of the menu and only on the top level. This suggests the need to re-work the walker to go back through and sort the menu once again.

    Furthermore, the only method I can find for setting the parent ID (thus allowing new submenu items) is by using the wp_update_nav_menu_item function. However, this only results in the new menu items being listed as “pending” on the admin GUI menu interface.

    So my issues are the following:

    • Need to add dynamic links/buttons to the menu
    • Need ability to sort these new links
    • Need ability to place certain links as submenu

    Any help would be much appreciated!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Dynamic menu options based on user’ is closed to new replies.