WordPress.org

Ready to get started?Download WordPress

Forums

Dynamic menu options based on user (3 posts)

  1. CGAdmin
    Member
    Posted 1 year ago #

    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()) {
      switch_to_blog(1);
      wp_nav_menu(array(
        'theme_location' => 'user-menu',
        'container_class' => 'nav-center',
    ));
      restore_current_blog();
    } else {
      switch_to_blog(1);
      wp_nav_menu(array(
        'theme_location' => 'public-menu',
        'container_class' => 'nav-center',
    ));
      restore_current_blog();
    } ?>

    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:
    "http://mysite.com/john-doe/link-1"
    "http://mysite.com/john-doe/link-2"

    And if user jane-smith is logged in, the same menu locations should instead point to:
    "http://mysite.com/jane-smith/link-1"
    "http://mysite.com/jane-smith/link-2"

    Would a custom walker allow for this?

  2. CGAdmin
    Member
    Posted 1 year ago #

    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' );
  3. CGAdmin
    Member
    Posted 1 year ago #

    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!

Topic Closed

This topic has been closed to new replies.

About this Topic