• Hi,

    I’m on WP 4.4 running with a Twentysixteen child theme. I’m displaying a custom menu in my sidebar using the Custom Menu widget. I want to show a menu item only for Authors, Editors & Administrators in the menu. To do this, I have added a function in my child theme’s functions.php. The code for this function is as follows…

    function add_dynamic_link ($items, $args) {
    	if (current_user_can ('publish_posts')) {
    		if ($args->menu == 'Sidebar') {
    			$items .= '<li><a href="/teamspace">Teamspace</a></li>';
    		}
    	}
    	return $items;
    }
    
    add_filter ('wp_nav_menu_items', 'add_dynamic_link', 10, 2);

    However, this does not have the desired result. The menu remains unchanged. I tried debugging the function by adding the following line of code at different points in the function.

    echo $items;

    From the debugging process, I could gather that the filter is being executed. The problem seems to be that the execution flow is not entering the following “IF” construct…

    if ($args->menu == 'Sidebar') {
    			$items .= '<li><a href="/teamspace">Teamspace</a></li>';
    		}

    Now, there exists a custom menu called Sidebar on my install. So this should not be happening. I tried changing the $args properties being validated. Instead of $args->menu == ‘Sidebar’ I tried using $args->menu == ‘menu-sidebar’ (the ID of the custom menu) as well as $args->container_class == ‘menu-sidebar-container’ but none of them works.

    If I remove the problematic “IF” construct, the menu item shows in all the active menus on the site…not only the one in the widget.

    What could I be doing wrong and how do I rectify it?

    Thanks

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    You just haven’t hit on the proper value to check for.

    You could do print_r( $args, true ); in your function and concatenate the return to the filter return. Include some <pre> tags as well so the output is formatted better. By seeing what you have to work with you can then decide what to check for in your conditional.

Viewing 1 replies (of 1 total)
  • The topic ‘Dynamic menu-items in Custom Menu widget’ is closed to new replies.