Support » Plugins » Hacks » wp_nav_menu() fallback not receiving vars

  • Resolved Edward McIntyre


    I developed a custom walker class to implement the Bootstrap 3.0 menu structure so menus can be edited using the WordPress menu editor.

    Everything works great except for the $fallback_cb which is used by default when the theme is installed. I read in the codex that

    (string) (optional) If the menu doesn’t exist, the fallback function to use. Set to false for no fallback. Note: Passes $args to the custom function.

    The issue seems to be that the wp_page_menu function is not receiving the $args from the wp_nav_menu() declaration.

    Here is my current wp_nav_menu() declaration.

    wp_nav_menu( array(
          'menu'           => 'primary',
          'theme_location' => 'primary',
          'depth'          => 2,
          'container'      => false,
          'menu_class'     => 'nav navbar-nav',
          'fallback_cb'    => 'wp_page_menu',
          'walker'         => new wp_bootstrap_navwalker())

    Can anyone offer some insight into how I might resolve this issue?

Viewing 5 replies - 1 through 5 (of 5 total)
  • esmi


    Forum Moderator

    The idea with the fallback_cb is that you set up your own fallback function – which may (or may not) make use of wp_page_menu().

    I definitely understand that, is guess the question is fallback_cb is undefined and defaults to wp_page_menu is there any way to pass the defined wp_nav_menu vars over to wp_page_menu?



    Forum Moderator

    From what I recall, wp_nav_menu() uses slightly different parameters to wp_page_menu(). So I’d guess that there no easy way of simply passing them from one function to the other.

    Moderator bcworkz


    It is true that wp_page_menu() uses different parameters, but any arguments you supply to wp_nav_menu() will be passed to the ‘fallback_cb’ function. Whether your ‘fallback_cb’ function or wp_page_menu() can make use of these arguments is another story.

    The key line in wp_nav_menu() source code is:
    call_user_func( $args->fallback_cb, (array) $args );

    where $args is the merge of passed and default arguments, so you can see all arguments are passed to the ‘fallback_cb’ function. If this does not appear to be happening, it could be the wp_page_menu() does not know how to use a particular argument, but the argument is being passed none the less.

    Interesting. Looks like the $args are being passed to my wp_page_menu() function. After some further digging I notice that does not accept the container & walker parameters. Time to write my own.

    Thanks for sharing your knowledge!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘wp_nav_menu() fallback not receiving vars’ is closed to new replies.