Make WordPress Core

Changeset 59177


Ignore:
Timestamp:
10/05/2024 10:08:06 PM (2 months ago)
Author:
joedolson
Message:

Menus: Remove redundant title attributes.

Omit title attributes if they are defined but are the same text as the menu item title, either before or after filtering. If a navigation menu filter makes significant changes to the menu title without changing the title attribute, this will still remove them. The cases where this occurs and the title attribute is still a useful value will be very uncommon, however.

Props hareesh-pillai, audrasjb, sabernhardt, afercia, sergeybiryukov, tirth03, joedolson.
Fixes #51299.

File:
1 edited

Legend:

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

    r59120 r59177  
    127127     * @since 5.9.0 Renamed `$item` to `$data_object` and `$id` to `$current_object_id`
    128128     *              to match parent class for PHP 8 named parameter support.
     129     * @since 6.7.0 Removed redundant title attributes.
    129130     *
    130131     * @see Walker::start_el()
     
    213214        $output .= $indent . '<li' . $li_attributes . '>';
    214215
     216        /** This filter is documented in wp-includes/post-template.php */
     217        $title = apply_filters( 'the_title', $menu_item->title, $menu_item->ID );
     218
     219        /**
     220         * Filters a menu item's title.
     221         *
     222         * @since 4.4.0
     223         *
     224         * @param string   $title     The menu item's title.
     225         * @param WP_Post  $menu_item The current menu item object.
     226         * @param stdClass $args      An object of wp_nav_menu() arguments.
     227         * @param int      $depth     Depth of menu item. Used for padding.
     228         */
     229        $title = apply_filters( 'nav_menu_item_title', $title, $menu_item, $args, $depth );
     230
    215231        $atts           = array();
    216         $atts['title']  = ! empty( $menu_item->attr_title ) ? $menu_item->attr_title : '';
    217232        $atts['target'] = ! empty( $menu_item->target ) ? $menu_item->target : '';
    218233        $atts['rel']    = ! empty( $menu_item->xfn ) ? $menu_item->xfn : '';
     
    229244
    230245        $atts['aria-current'] = $menu_item->current ? 'page' : '';
     246
     247        if ( ! empty( $menu_item->attr_title )
     248            && trim( strtolower( $menu_item->attr_title ) ) !== trim( strtolower( $title ) )
     249        ) {
     250            $atts['title'] = $menu_item->attr_title;
     251        } else {
     252            $atts['title'] = '';
     253        }
    231254
    232255        /**
     
    252275        $attributes = $this->build_atts( $atts );
    253276
    254         /** This filter is documented in wp-includes/post-template.php */
    255         $title = apply_filters( 'the_title', $menu_item->title, $menu_item->ID );
    256 
    257         /**
    258          * Filters a menu item's title.
    259          *
    260          * @since 4.4.0
    261          *
    262          * @param string   $title     The menu item's title.
    263          * @param WP_Post  $menu_item The current menu item object.
    264          * @param stdClass $args      An object of wp_nav_menu() arguments.
    265          * @param int      $depth     Depth of menu item. Used for padding.
    266          */
    267         $title = apply_filters( 'nav_menu_item_title', $title, $menu_item, $args, $depth );
    268 
    269277        $item_output  = $args->before;
    270278        $item_output .= '<a' . $attributes . '>';
Note: See TracChangeset for help on using the changeset viewer.