How to extend nav-menu-item-control data in Appearance > Customize?

I have one question as for Nav Menu Items custom fields in the Appearance > Customizer screen.

Since 5.4. there are 2 new hooks:

  • wp_nav_menu_item_custom_fields – it works for Appearance > Menus properly and I have added 2 custom fields.
  • wp_nav_menu_item_custom_fields_customize_template – it works for Appearance > Customizer and adds HTML properly.

enter image description here

But I need to use {{data}} attribute for getting menu item attributes (my custom e.g. {{{data.visibility}}}). I want to put the same 2 custom fields to WP Customizer > Menus > Item section via this hook wp_nav_menu_item_custom_fields_customize_template and use in JS extended {{{data}}} object.

There is default {{data}} variable in JS template, it looks like this object

enter image description here

I have added 2 custom fields with custom data for each menu item via this hook wp_nav_menu_item_custom_fields and it works properly.

Is there a way how I can add custom properties for every menu item to {data} via PHP (add_filter) or JS hooks (wp.hooks.addFilter). I don’t want to localize script and create another variable and use it in the JS template in wp_nav_menu_item_custom_fields_customize_template hook’s callback.

Let me know,
Thanks!

0

Leave a Comment