Skip to content

Commit 6c285f0

Browse files
committed
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. git-svn-id: https://develop.svn.wordpress.org/trunk@59179 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 7221eb1 commit 6c285f0

File tree

1 file changed

+30
-16
lines changed

1 file changed

+30
-16
lines changed

src/wp-includes/class-walker-nav-menu.php

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ public function end_lvl( &$output, $depth = 0, $args = null ) {
126126
* @since 4.4.0 The {@see 'nav_menu_item_args'} filter was added.
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()
131132
*
@@ -212,8 +213,25 @@ public function start_el( &$output, $data_object, $depth = 0, $args = null, $cur
212213

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+
// Save filtered value before filtering again.
220+
$the_title_filtered = $title;
221+
222+
/**
223+
* Filters a menu item's title.
224+
*
225+
* @since 4.4.0
226+
*
227+
* @param string $title The menu item's title.
228+
* @param WP_Post $menu_item The current menu item object.
229+
* @param stdClass $args An object of wp_nav_menu() arguments.
230+
* @param int $depth Depth of menu item. Used for padding.
231+
*/
232+
$title = apply_filters( 'nav_menu_item_title', $title, $menu_item, $args, $depth );
233+
215234
$atts = array();
216-
$atts['title'] = ! empty( $menu_item->attr_title ) ? $menu_item->attr_title : '';
217235
$atts['target'] = ! empty( $menu_item->target ) ? $menu_item->target : '';
218236
$atts['rel'] = ! empty( $menu_item->xfn ) ? $menu_item->xfn : '';
219237

@@ -229,6 +247,17 @@ public function start_el( &$output, $data_object, $depth = 0, $args = null, $cur
229247

230248
$atts['aria-current'] = $menu_item->current ? 'page' : '';
231249

250+
// Add title attribute only if it does not match the link text (before or after filtering).
251+
if ( ! empty( $menu_item->attr_title )
252+
&& trim( strtolower( $menu_item->attr_title ) ) !== trim( strtolower( $menu_item->title ) )
253+
&& trim( strtolower( $menu_item->attr_title ) ) !== trim( strtolower( $the_title_filtered ) )
254+
&& trim( strtolower( $menu_item->attr_title ) ) !== trim( strtolower( $title ) )
255+
) {
256+
$atts['title'] = $menu_item->attr_title;
257+
} else {
258+
$atts['title'] = '';
259+
}
260+
232261
/**
233262
* Filters the HTML attributes applied to a menu item's anchor element.
234263
*
@@ -251,21 +280,6 @@ public function start_el( &$output, $data_object, $depth = 0, $args = null, $cur
251280
$atts = apply_filters( 'nav_menu_link_attributes', $atts, $menu_item, $args, $depth );
252281
$attributes = $this->build_atts( $atts );
253282

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-
269283
$item_output = $args->before;
270284
$item_output .= '<a' . $attributes . '>';
271285
$item_output .= $args->link_before . $title . $args->link_after;

0 commit comments

Comments
 (0)