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
  • That filter doesn't exist in WP Core, it only exists in ACF, you should consult the ACF docs, you would need to create a custom walker that calls that filter yourself. Also keep in mind that menus are going to be a part of the block editor in the future – Tom J Nowell May 13 '19 at 10:12
  • I believe this action also exists in the Nav Menu Roles plugin (but again, not part of WP Core). – butlerblog Jul 11 '19 at 20:19
  • Update to my previous comment - it seems to exist in around 60-70 plugins and themes (even though it's not part of WP Core). Seems that somewhere along the line, someone made it up for their own use case, and others copied that use case, thinking that action hook was an actual hook. Regardless, including it in a nav walker does seem to allow for compatibility with other plugins/themes using the same hook. – butlerblog Jan 13 at 21:22

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.

|improve this answer|||||

Add this after your function;


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

|improve this answer|||||

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.