[wp-trac] [WordPress Trac] #47720: Walker_Nav_Menu filter nav_menu_link_attributes checks $atts argument via empty() and not isset(), causing confusion with legitimate values that evaluate to boolean false

WordPress Trac noreply at wordpress.org
Sun Jul 28 17:48:43 UTC 2019


#47720: Walker_Nav_Menu filter nav_menu_link_attributes checks $atts argument via
empty() and not isset(), causing confusion with legitimate values that
evaluate to boolean false
-------------------------------------+------------------------------
 Reporter:  nevma                    |       Owner:  (none)
     Type:  defect (bug)             |      Status:  new
 Priority:  normal                   |   Milestone:  Awaiting Review
Component:  Menus                    |     Version:  5.2.2
 Severity:  normal                   |  Resolution:
 Keywords:  has-patch needs-testing  |     Focuses:  template
-------------------------------------+------------------------------

Comment (by greenshady):

 Just ran into this problem myself.

 In a similar attribute system I built, here's how I handle it, which also
 allows attributes without a value (where the value would be the same as
 the attribute name):

 {{{
 foreach ( $this->all() as $name => $value ) {

         $esc_value = '';

         // If the value is a link `href`, use `esc_url()`.
         if ( $value !== false && 'href' === $name ) {
                 $esc_value = esc_url( $value );

         } elseif ( $value !== false ) {
                 $esc_value = esc_attr( $value );
         }

         $html .= false !== $value ? sprintf( ' %s="%s"', $name, $esc_value
 ) : " {$name}";
 }
 }}}

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


More information about the wp-trac mailing list