[wp-trac] [WordPress Trac] #43113: Multiple custom item classes are returned as single string when using 'nav_menu_link_attributes' filter with Customizer preview

WordPress Trac noreply at wordpress.org
Fri Feb 23 23:51:14 UTC 2018


#43113: Multiple custom item classes are returned as single string when using
'nav_menu_link_attributes' filter with Customizer preview
--------------------------+------------------------------
 Reporter:  lrdn          |       Owner:
     Type:  defect (bug)  |      Status:  new
 Priority:  normal        |   Milestone:  Awaiting Review
Component:  Customize     |     Version:  4.9.2
 Severity:  normal        |  Resolution:
 Keywords:                |     Focuses:
--------------------------+------------------------------

Comment (by lrdn):

 Weston, thanks for taking a look. Simply add a
 {{{nav_menu_link_attributes}}} filter function and log the classes array
 of the item object. I am using the default logger in my example.

 {{{#!php
 <?php

 function custom_link_attributes($wp_attributes, $wp_item, $wp_args)
 {
         error_log(json_encode($wp_item->classes, JSON_PRETTY_PRINT));
         return $wp_attributes;
 }
 add_filter('nav_menu_link_attributes', 'custom_link_attributes', 10, 3);
 }}}

 Now edit an existing menu item using the Customizer and define at least 2
 custom CSS classes. The log output will show both classes located within
 the same array item.

 {{{
 [
     "custom-class-1 custom-class-2",
     "menu-item",
     ...
 ]
 }}}

 As soon as the changes are published, or whenever the menu items are
 modified in the appearance settings, the item classes will be located in
 separate array items as expected.

 {{{
 [
     "custom-class-1",
     "custom-class-2",
     "menu-item",
     ...
 ]
 }}}

 My patch is the earliest point at which I could locate and correct the
 malformed data, but I am guessing the cause is actually located where the
 string is handed over by the JavaScript request.

--
Ticket URL: <https://core.trac.wordpress.org/ticket/43113#comment:2>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list