0

I am trying to manipulate the existing primary menu with a plugin.

The result should be: if the plugin is activated, it edits the 'Login' button to be conditional:

if something is true
     show profile picture and name
else
     show login button

There is still a long way to that result, right now I'm trying to generally manipulate the main menu with a custom Walker class

Here is my code:

add_action( 'wp_loaded', 'edit_that_shit');

function edit_that_shit(){
    wp_nav_menu(
        array(
            'theme_location' => 'primary',
            'menu' => 'Primary Navigation',
            'walker' => new Custom_Walker()
        )
    );
}


class Custom_Walker extends Walker_Nav_Menu
{
     public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 )
     { ... }
}

But the only thing that happens is, that WordPress creates a new menu beside the main menu, with the same items on top of the page.

1
  • If you want to modify the arguments of an already existing menu, better use the wp_nav_menu_args filter. There is another filter, wp_nav_menu_items, to modify the final html output. – Luis Sanz Jun 10 '16 at 10:32
2

Given that you want to include a picture in the wp_nav_menu output, your best bet is to use the wp_nav_menu_items filter, as suggested in the comments. This will allow you to do a search and replace on the html.

add_filter ('wp_nav_menu_items','wpse229358_filter_menu');
function wpse229358_filter_menu ($items,$args) {
  $search  = ''; // html string of button
  $replace = ''; // html string of picture+name
  if (yourcondition) {
    str_replace ($search,$replace,$items);
    }
  }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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