[wp-trac] [WordPress Trac] #37136: 'nav_menu_link_attributes' filter is called with $args as an object instead of an array

WordPress Trac noreply at wordpress.org
Tue Jun 21 09:13:40 UTC 2016


#37136: 'nav_menu_link_attributes' filter is called with $args as an object instead
of an array
--------------------------+-----------------------------
 Reporter:  Fab1en        |      Owner:
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  Menus         |    Version:  trunk
 Severity:  normal        |   Keywords:
  Focuses:  template      |
--------------------------+-----------------------------
 When I register the `nav_menu_link_attributes` filter, the third argument
 `$args` is an object. But
 [http://codex.wordpress.org/Plugin_API/Filter_Reference/nav_menu_link_attributes
 the documentation] says it should be an array.

     $atts - HTML attributes in an associative array
     $item - Object containing item details. E.G: If the link is to a page
 $item will be a WP_Post object
     $args - Array containing config with desired markup of nav item

 You can see later on
 [http://codex.wordpress.org/Plugin_API/Filter_Reference/nav_menu_link_attributes#Examples
 the same documentation page] that `$args` is an object :


 {{{
 function add_specific_menu_location_atts( $atts, $item, $args ) {
     // check if the item is in the primary menu
     if( $args->theme_location == 'primary' ) {
       // add the desired attributes:
       $atts['class'] = 'menu-link-class';
     }
     return $atts;
 }
 add_filter( 'nav_menu_link_attributes', 'add_specific_menu_location_atts',
 10, 3 );
 }}}

 Notice the `$args->theme_location` instead of `$args['theme_location']`.

 That inconsistency would not bother me if I was not providing a fallback
 function for `wp_nav_menu`. In the fallback function, I call
 `walk_page_tree`, which in turn calls the `nav_menu_link_attributes`
 filter, but with `$args` set as an array.

 {{{
 wp_nav_menu( array(
     'theme_location' => 'main',
     'fallback_cb' => 'my_default_menu'
 ) );

 function my_default_menu( $args = array() ) {
     // get all published posts
     $list_args = $args;
     $list_args['echo'] = false;
     $list_args['walker'] = new Walker_Nav_Menu;
     $posts = get_posts( array( 'nopaging' => true ) );

     // generate menu HTML
     $menu = walk_page_tree( $posts, 0, 0, $list_args );
     if ( $args['echo'] ) echo $menu;
     else return $menu;
 }
 }}}

 I cannot trace down where the array to object conversion is done.

--
Ticket URL: <https://core.trac.wordpress.org/ticket/37136>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list