Using string instead of object class instantiation on the walker argument breaks wp_nav_menu

I’m trying to replace the walker argument from new My_Walker_Nav_Menu() to a string ‘My_Walker_Nav_Menu’ in this wp_nav_menu call (which works as is)

wp_nav_menu(
array(
‘theme_location’ => ‘header_nav’,
‘menu_class’ => ‘main-menu’,
‘container’ => ”,
‘fallback_cb’ => false,
‘walker’ => new My_Walker_Nav_Menu()
)
);

When I change it to a string I’m getting this error Fatal error: Using $this when not in object context

The class is basic:

class My_Walker_Nav_Menu extends Walker_Nav_Menu {
function start_lvl(&$output, $depth = 0, $args = Array()) {
$indent = str_repeat(“t”, $depth);
if(‘header_nav’ == $args->theme_location ){
$output .='<span class=”toggle-submenu fa fa-angle-down”></span>’;
}
$output .= “n$indent<ul class=”sub-menu”>n”;
}
}

I need to do this because apparently Customizer’s partial refresh doesn’t work with custom walkers.

When the Customizer determines it can’t do a partial refresh, it falls back to performing a full page refresh. Conditions for why a menu change will get a full page refresh include:

or if wp_nav_menu() is called with with a walker object instance, as opposed to a class name string;

Somebody else had this issue (mentioned in a comment) but didn’t get an answer.
I’ve searched through the docs but can’t seem to find anything.

Any help is much appreciated.

Read more here:: Using string instead of object class instantiation on the walker argument breaks wp_nav_menu

Leave a Reply

Your email address will not be published. Required fields are marked *