• I’m working on a project where I need a dynamic menu link item that goes to site.com/username (for logged in users) and can’t figure out how to make that happen. I’ve tried a few things, but no luck. Can anyone help? Thanks in advance!

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator bcworkz

    (@bcworkz)

    Assuming your theme uses wp_nav_menu() to output the menu (as most do), its HTML can be altered through the “wp_nav_menu” filter. An additional menu item’s HTML could be dynamically inserted. You can get the current logged in user’s username (user_nicename property) from get_userdata( get_current_user_id()).

    Thread Starter SR20DETg20girl

    (@sr20detg20girl)

    Thank you for confirming that it can be done, but I’m still not sure HOW to do that. Guess I’m not advanced enough to figure it out on my own.

    Moderator bcworkz

    (@bcworkz)

    Basically this:

    add_filter('wp_nav_menu', function( $menu, $args ) {
      $user = get_current_user_id();
      if ( 0 < $user ) {
         $nicename = get_userdata( $user )->data->user_nicename;
         $link = get_site_url() .'/author/'. $nicename . '/';
         $menu = str_replace('</ul></div>', '<li id="menu-item" class="menu-item menu-item-type-custom menu-item-object-custom menu-item">
           <a href="'. $link .'">Your Page</a></li></ul></div>', $menu );
      }
      return $menu;
    }, 10, 2 );

    Place it in your theme’s functions.php file, though if your theme is subject to periodic updates, it’ll be overwritten during the update. To protect this code from updates, it could be placed into a simple, custom, standalone plugin (or child theme, but plugins are easier to create).

    You may need to alter the HTML (the parts within ‘single quotes’) some to fit the specifics of your site. I did verify it works on my site using the twentytwentyone theme.

    Thread Starter SR20DETg20girl

    (@sr20detg20girl)

    OMG you are the most wonderful person in the history of the world, THANK YOU!! That was a huge help. I was able to make a few minor tweaks and it functions perfectly! Now just gotta figure out how to stick it at the front/left of the menu instead of the end/right. Is there some way I can throw you a few dollars for your work? ??

    Moderator bcworkz

    (@bcworkz)

    I appreciate the thought but offering any sort of compensation between forum members is not allowed by our guidelines. It tends to bring out bad actors. They are why civil people sometimes can’t do nice things for others.

    The placement of the added link is governed by the first argument (what to search for) of the str_replace() function. I used '</ul></div>', which occurs at the end of the menu’s HTML. The corresponding opening tags would be used to place the link in front. It has to be an exact match, so you’d include all the div tag’s attributes so the subsequent ul tag can be included.

    The second argument is what replaces the first, so you’d need to include all of the first argument in the second to maintain the original HTML. I included the first at the end of the replacement. You’d instead include it at the start. There is a third argument, $menu, don’t lose that one among all the other HTML you’d be changing ??

    Instead of doing that, it should be possible to rearrange the order of menu elements with CSS alone. The CSS flex box model has an order: property that can be used to alter the order elements appear in.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Custom site.com/username link in menu?’ is closed to new replies.