0

I'm trying to add a menu using a custom walker class into a page or post body and settled on using a shortcode. I don't have to use a shortcode, but I do have to embed the menu into the central portion of page/post content, and the menu must use a custom walker.

While questions like this one: https://wordpress.stackexchange.com/a/293006/67420 have a working method to generally embed a menu inside page content, trying to extend that to also pass a custom walker does fail, with this warning:

Warning: call_user_func_array() expects parameter 1 to be a valid callback, class 'new wp_bootstrap_navwalker()' not found in /.../site/wp-includes/nav-menu-template.php on line 580

This menu, using this same custom walker class, runs fine when called regularly in the header. In this certain case, I must insert the main menu into the central portion of a certain page's content, instead (and so it's not called in that page's header). Also, I've tried to explicitly include the class as suggested below by @Antti Koskinen.

Here's my shortcode:

[menu container="div" container_id="main-nav" container_class="collapse navbar-collapse justify-content-center" menu_id="false" menu_class="navbar-nav" depth="3" fallback_cb="wp_bootstrap_navwalker::fallback" walker="new wp_bootstrap_navwalker()"]

And here's my custom shortcode function:

add_shortcode( 'menu', 'se_menu_shortcode' );

function se_menu_shortcode( $atts, $content = null ) {

    extract( shortcode_atts( array(  
        'container' => null,
        'container_id' => null,
        'container_class' => null,
        'menu_id' => null,
        'menu_class' => null,
        'depth' => null,
        'fallback_cb' => null,
        'walker' => null 
    ), $atts ) );

    return wp_nav_menu( array(  
        'container' => $container,
        'container_id' => $container_id,
        'container_class' => $container_class,
        'menu_id' => $menu_id,
        'menu_class' => $menu_class,
        'depth' => $depth,
        'fallback_cb' => $fallback_cb,
        'walker' => $walker,
        'echo' => false 
    ) );
}

Appreciate any help!

1 Answer 1

1

Update 30.6.2019

Ah, now I think understand what is going on with the code and the warning. I think it is literally trying to find a class named "new wp_bootstrap_navwalker()".

In my local sandbox WP I got the shortcode working after I changed 'walker' => $walker, to 'walker' => new $walker,. I also changed the walker parameter from walker="new wp_bootstrap_navwalker()" to walker="wp_bootstrap_navwalker"

EDIT

'walker' => new $walker, should probably be something like 'walker' => $walker ? new $walker : '', to avoid errors, if $walker is null.


If you're getting a "class not found" warning, then maybe the class file hasn't just been included/loaded when you're trying to instantiate the class. So perhaps you could try requiring/including the class file inside your shortcode function when it's needed. Something along these lines,

function se_menu_shortcode( $atts, $content = null ) {

  // extract

  if ( ! class_exists('wp_bootstrap_navwalker') ) {
    require_once 'path/to/class/definition/file.php';
  }

  // return
}

Or maybe you could try writing an autoloader for handling the requiring/including of classes automatically, https://www.php.net/manual/en/function.spl-autoload-register.php

3
  • Thanks for the reply. Should have mentioned I'd already tried explicitly including the class similar to what you've suggested - and confirmed doing so doesn't resolve or effect this warning. In fact, it's worth noting I can load this same menu as a regular menu just fine. And I can load this menu in page/post content with the shortcode using the standard walker (by way of not specifying a walker at all) just fine. Just not able to get it embedded into page/post content using my preferred custom walker by way of a shortcode, for whatever reason, so far.
    – rambillo
    Commented Jun 30, 2019 at 1:24
  • @rambillo I tested your code more thoroughly on my local WP install and updated my answer with my findings. Can you get your code working, if you make the same changes to it as I did? Commented Jun 30, 2019 at 7:50
  • Yes! Revising the walker element and shortcode parameter as you've suggested (and validating $walker is not null) resolves the warning and successfully pulls the menu -- with my custom walker -- into the page content! Thank you!
    – rambillo
    Commented Jul 1, 2019 at 15:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.