Make WordPress Core


Ignore:
Timestamp:
07/06/2020 08:42:14 PM (4 years ago)
Author:
afercia
Message:

Accessibility: Widgets: Add theme support to make widgets output list of links wrapped within a <nav> element.

Widgets that output list of links can now be wrapped within <nav> elements to improve semantics and accessibility.

The <nav> elements are also native landmark regions, which helps assistive technology users to navigate through them. Themes can opt-in to this new behavior by declaring support for the new html5 feature navigation-widgets.

Props joedolson, simonjanin, audrasjb, afercia.
Fixes #48170.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/widgets/class-wp-widget-recent-comments.php

    r48109 r48349  
    8383        $output = '';
    8484
    85         $title = ( ! empty( $instance['title'] ) ) ? $instance['title'] : __( 'Recent Comments' );
     85        $default_title = __( 'Recent Comments' );
     86        $title         = ( ! empty( $instance['title'] ) ) ? $instance['title'] : $default_title;
    8687
    8788        /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
     
    124125        $first_instance     = false;
    125126
     127        $format = current_theme_supports( 'html5', 'navigation-widgets' ) ? 'html5' : 'xhtml';
     128
     129        /**
     130         * Filters the HTML format of widgets with navigation links.
     131         *
     132         * @since 5.5.0
     133         *
     134         * @param string $format The type of markup to use in widgets with navigation links.
     135         *                       Accepts 'html5', 'xhtml'.
     136         */
     137        $format = apply_filters( 'navigation_widgets_format', $format );
     138
     139        if ( 'html5' === $format ) {
     140            // The title may be filtered: Strip out HTML and make sure the aria-label is never empty.
     141            $title      = trim( strip_tags( $title ) );
     142            $aria_label = $title ? $title : $default_title;
     143            $output    .= '<nav role="navigation" aria-label="' . esc_attr( $aria_label ) . '">';
     144        }
     145
    126146        $output .= '<ul id="' . esc_attr( $recent_comments_id ) . '">';
    127147        if ( is_array( $comments ) && $comments ) {
     
    142162        }
    143163        $output .= '</ul>';
     164
     165        if ( 'html5' === $format ) {
     166            $output .= '</nav>';
     167        }
     168
    144169        $output .= $args['after_widget'];
    145170
Note: See TracChangeset for help on using the changeset viewer.