💥 TRENDING: Changeset/ - High Quality

Changeset 56067


Ignore:
Timestamp:
06/27/2023 03:25:00 PM (3 years ago)
Author:
audrasjb
Message:

Menus: Allow themes and plugins to pass HTML attributes to various Nav Walker outputs.

This introduces a new set of hooks that can be used to filter various HTML elements of the Nav Walker, in order to output the desired HTML attributes:

  • List items: nav_menu_item_attributes
  • Submenu <ul> element: nav_menu_submenu_attributes

Props davidwebca, danyk4, costdev, peterwilsoncc, audrasjb, oglekler.
Fixes #57140.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-walker-nav-menu.php

    r55261 r56067  
    7474         */
    7575        $class_names = implode( ' ', apply_filters( 'nav_menu_submenu_css_class', $classes, $args, $depth ) );
    76         $class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
    77 
    78         $output .= "{$n}{$indent}<ul$class_names>{$n}";
     76
     77        $atts          = array();
     78        $atts['class'] = ! empty( $class_names ) ? $class_names : '';
     79
     80        /**
     81         * Filters the HTML attributes applied to a menu list element.
     82         *
     83         * @since 6.3.0
     84         *
     85         * @param array $atts {
     86         *     The HTML attributes applied to the `<ul>` element, empty strings are ignored.
     87         *
     88         *     @type string $class    HTML CSS class attribute.
     89         * }
     90         * @param stdClass $args      An object of `wp_nav_menu()` arguments.
     91         * @param int      $depth     Depth of menu item. Used for padding.
     92         */
     93        $atts       = apply_filters( 'nav_menu_submenu_attributes', $atts, $args, $depth );
     94        $attributes = $this->build_atts( $atts );
     95
     96        $output .= "{$n}{$indent}<ul{$attributes}>{$n}";
    7997    }
    8098
     
    157175         */
    158176        $class_names = implode( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $menu_item, $args, $depth ) );
    159         $class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
    160177
    161178        /**
     
    171188         */
    172189        $id = apply_filters( 'nav_menu_item_id', 'menu-item-' . $menu_item->ID, $menu_item, $args, $depth );
    173         $id = $id ? ' id="' . esc_attr( $id ) . '"' : '';
    174 
    175         $output .= $indent . '<li' . $id . $class_names . '>';
     190
     191        $li_atts          = array();
     192        $li_atts['id']    = ! empty( $id ) ? $id : '';
     193        $li_atts['class'] = ! empty( $class_names ) ? $class_names : '';
     194
     195        /**
     196         * Filters the HTML attributes applied to a menu's list item element.
     197         *
     198         * @since 6.3.0
     199         *
     200         * @param array $li_atts {
     201         *     The HTML attributes applied to the menu item's `<li>` element, empty strings are ignored.
     202         *
     203         *     @type string $class        HTML CSS class attribute.
     204         *     @type string $id           HTML id attribute.
     205         * }
     206         * @param WP_Post  $menu_item The current menu item object.
     207         * @param stdClass $args      An object of wp_nav_menu() arguments.
     208         * @param int      $depth     Depth of menu item. Used for padding.
     209         */
     210        $li_atts       = apply_filters( 'nav_menu_item_attributes', $li_atts, $menu_item, $args, $depth );
     211        $li_attributes = $this->build_atts( $li_atts );
     212
     213        $output .= $indent . '<li' . $li_attributes . '>';
    176214
    177215        $atts           = array();