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-meta.php

    r47593 r48349  
    4343     */
    4444    public function widget( $args, $instance ) {
    45         $title = ! empty( $instance['title'] ) ? $instance['title'] : __( 'Meta' );
     45        $default_title = __( 'Meta' );
     46        $title         = ! empty( $instance['title'] ) ? $instance['title'] : $default_title;
    4647
    4748        /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
     
    5354            echo $args['before_title'] . $title . $args['after_title'];
    5455        }
     56
     57        $format = current_theme_supports( 'html5', 'navigation-widgets' ) ? 'html5' : 'xhtml';
     58
     59        /**
     60         * Filters the HTML format of widgets with navigation links.
     61         *
     62         * @since 5.5.0
     63         *
     64         * @param string $format The type of markup to use in widgets with navigation links.
     65         *                       Accepts 'html5', 'xhtml'.
     66         */
     67        $format = apply_filters( 'navigation_widgets_format', $format );
     68
     69        if ( 'html5' === $format ) {
     70            // The title may be filtered: Strip out HTML and make sure the aria-label is never empty.
     71            $title      = trim( strip_tags( $title ) );
     72            $aria_label = $title ? $title : $default_title;
     73            echo '<nav role="navigation" aria-label="' . esc_attr( $aria_label ) . '">';
     74        }
    5575        ?>
    56             <ul>
     76
     77
     78        <ul>
    5779            <?php wp_register(); ?>
    5880            <li><?php wp_loginout(); ?></li>
    5981            <li><a href="<?php echo esc_url( get_bloginfo( 'rss2_url' ) ); ?>"><?php _e( 'Entries feed' ); ?></a></li>
    6082            <li><a href="<?php echo esc_url( get_bloginfo( 'comments_rss2_url' ) ); ?>"><?php _e( 'Comments feed' ); ?></a></li>
     83
    6184            <?php
    6285            /**
     
    81104            wp_meta();
    82105            ?>
    83             </ul>
    84             <?php
    85106
    86             echo $args['after_widget'];
     107        </ul>
     108
     109        <?php if ( 'html5' === $format ) : ?>
     110            </nav>
     111        <?php endif; ?>
     112
     113        <?php
     114        echo $args['after_widget'];
    87115    }
    88116
Note: See TracChangeset for help on using the changeset viewer.