0

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");
}
  • 1. use add_filter function instead of add_action 2. Use file_put_contents('log.text', "test test"); code to check calling function – MahdiY Mar 26 '17 at 13:11
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.

-1

Add this after your function;

do_action('my_custom_fields');

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

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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