1

I want to add a custom field in the menu, therefore I am trying to use wp_nav_menu_item_custom_fields. I am using following code but it does not even call the function. I am adding it in a plugin, why is wrong in it?

add_action( 'wp_nav_menu_item_custom_fields', 'my_custom_fields', 10, 4);

function my_custom_fields( $item_id, $item, $depth, $args ) {
    var_dump("test test");
}
qasir
  • 53
  • 1
  • 1
  • 4

2 Answers2

0

Currently it doesn't seem to exist a filter to manage fields in the menu creation. On the other hand, what you can do is to create a new "menu walker", to provide Wordpress input fields as well as adding your own. That involves the extension of Wordpress class Walker_Nav_Menu_Edit with your own code.

Since the explanation of all the needed code is a bit long, I'd like to suggest you to use an existing plugin and use the parts you need. I, for example, used the plugin Menu image as a starting point. Pay attention to the two classes there:

  • Menu_Image_Plugin creates the basic functionality of the plugin (you will only need some of the filters and actions in the constructor)
  • Menu_Image_Walker_Nav_Menu_Edit, which extends Walker_Nav_Menu_Edit, to create the fields for the menu

Hope this helps you (and maybe others) to manage their menus the way they want :)

UPDATE As always, while searching for something else, you find an answer for waht you were previously looking for. So.. here is an answer that could help you with a bit of (easy) code; here a more thorough answer, with more (easy-to-medium) code.

Erenor Paz
  • 449
  • 6
  • 10
-1

Add this after your function;

do_action('my_custom_fields');

This will force wp to complete all registered callbacks assigned to said action.

user657489
  • 11
  • 5