Dynamic Menu URL/Structure per 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.

I currently have one public menu and one user menu which is properly displaying over each child site. However, the user menu must be fluid/dynamic and create the hierarchy based upon that particular user. Additionally, I would like to relocate a few of the Buddypress links to my nav bar and remove the BP bar completely.

In other words, 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"

I believe I will need a custom walker for this, but am not entirely sure how to proceed. Any ideas?

  • aecnu

    Greetings CGAdmin,

    Welcome to WPMU Dev!

    Thank you for bringing this intriguing issue to our attention, it is certainly appreciated.

    Unfortunately I am not a coder myself and this obviously is of the custom coding league which though it is not a part of our support mandate, I will see if I can enlist the assistance of one of our coders in here for their advise.

    I will see if I can get the WPMU Dev lead developer @Aaron in here with his invaluable insight into coding for his advice/advise for us.

    Though this may take a bit longer then a normal ticket, I will try to get him in here asap.

    Cheers, Joe

  • CGAdmin

    I looked over the Custom Admin Bar plugin and it only pertains to the admin bar as would be reasonably expected. This really doesn't help me any. However, perhaps how the admin bar is created can somehow be duplicated on the menu structures within a page? It seems that this should somehow be possible...

    For instance, your plugin adds new links to the wp_admin_bar array and are subsequently included in the admin bar once it is generated. This is essentially the same concept behind any menu regardless of position on the page, but would use the menu walker instead. Considering that BP was able to dynamically create menus within the admin bar suggests that the same type of thing is possible with the other menus.

    My problem is that I do not fully understand the walkers or how they work. I have been able to add items to the menu, but they have yet to be functional or dynamic. Is this too difficult a problem for the coders here?

  • Tom Eagles

    @CGAdmin

    The admin bar can be cutomised per usertype to exclude or include user types. By using a combination of functions you could include or exclude menu items, and also by the same kind of method redirect users if they try to access a page such as dashboard or my sites etc if they dont have a certain access type. Most of the dynamic cutom menu types just reference the !current_user_can

    // show admin bar only for admins
    if (!current_user_can('manage_options')) {
    	add_filter('show_admin_bar', '__return_false');
    }
    // show admin bar only for admins and editors
    if (!current_user_can('edit_posts')) {
    	add_filter('show_admin_bar', '__return_false');
    }

    or you could just set the admin bar to be permanently displayed and add custom snippets to handle who sees what etc. (this also includes a login for anyone not logged in)

    // always show admin bar
    function my_login_adminbar( $wp_admin_bar) {
    	if ( !is_user_logged_in() )
    	$wp_admin_bar->add_menu( array( 'title' => __( 'Log In' ), 'href' => wp_login_url() ) );
    }
    add_action( 'admin_bar_menu', 'my_login_adminbar' );
    add_filter( 'show_admin_bar', '__return_true' , 1000 );

    to do the part you needed for individual users then use something like this. (buddypress example)

    // Add buddypress profile link and buddypress activity links to wordpress menu
    
    add_filter( 'wp_nav_menu_items', 'my_buddypress_links',);
    function my_buddypress_links($menu) {
    if (!is_user_logged_in())
    return $menu;
    else
    $profilelink = '<li><a href="' . bp_loggedin_user_domain( '/' ) . '">' . __('My Profile', 'buddypress' ) . '</a></li>';
    $activitylink ='<li><a href ="' bp_loggedin_user_domain() . '/activity/'"> . _('My activities' , 'buddypress') . '</a></li>';
    $menu['profile_link']=$profilelink;
    $menu['activity_link']=$activitylink;
    return $menu;
    }

    this would do what you wanted in a buddypress environmnent you would just need to change the urls etc.

  • aecnu

    Greetings @tom.eagles,

    Fantastic post and thank you for that though I admit I am not a coder.

    We do appreciate your trying to help another member with custom coding.

    @CGAdmin you are requesting/desiring custom coding, but that is not within the scope of the support we provide here nor our support mission.

    The reason why I point this out was because that is exactly what I was told when I was asked to work in support, to help members anyway that I can to the best of my ability with our plugins and themes as they are out of the box and if I know more then that to help with WordPress issues to offer that as well.

    Anything more the support team would need to be all coders and the rate a month would almost need to be per hour to be able to support the coders in this manner on both the support team as well as the plugin developer and theme developer teams.

    Obviously that is not going to happen.

    Thank you both for being WPMU Dev Community Members!

    Cheers, Joe

  • CGAdmin

    @tom.eagles

    Thanks for the suggestions. My menus are already divided between public and user menus using slightly different methodology, though your suggestions are excellent as well. Your Buddypress code gets a little closer, though I have been unable to determine how to make this work with an existing menu since all it does is add a specific link the menu.

    For instance, this is the code I have toyed with previously, but have only been able to add the new items to either the front or end of the existing menu. I cannot place a new item as a submenu selection of a previously existing button.

    function new_nav_menu_items($items) {
        global $user_login;
    
    	if (!is_user_logged_in()){
    		return $items;
    	}
    	$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' );

    This is what makes me believe the solution will have something to do with a custom walker. Thanks for your input though.

    @aecnu

    Ahh yes, the infamous "support mandate." It's funny how that only shows up after I pay for the so-called "unlimited ... 24/7/365 Support ... about anything at all to do with WordPress." I suppose it is true that I can ask anything I want, but I will simply receive the reply that it is not your job to answer my questions. As I said previously, I am not asking for you to write my code; I am simply trying to figure out how to get Wordpress to do what I desire. This was the entire reason why I purchased a membership.

  • Tom Eagles

    Greetings and thanks for being a great community member.
    We haven't heard from you on this one for long and I am doing a regular followup to see if there is still something we can assist you on this thread.
    Just to manage the support issues more efficiently, I am marking this thread as resolved for now however this is not being done to avoid your questions in any way.
    Please feel free to mark this is "Not resolved" in case you have further questions and we would be back on it.
    Thanks a lot for being with WPMU DEV.
    Cheers!
    Tom
    Sales &Support
    Did you know we offer FREE lifetime memberships? Click here to learn more.

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.