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

    r48111 r48349  
    4545        static $first_dropdown = true;
    4646
    47         $title = ! empty( $instance['title'] ) ? $instance['title'] : __( 'Categories' );
     47        $default_title = __( 'Categories' );
     48        $title         = ! empty( $instance['title'] ) ? $instance['title'] : $default_title;
    4849
    4950        /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
     
    110111            <?php
    111112        } else {
     113            $format = current_theme_supports( 'html5', 'navigation-widgets' ) ? 'html5' : 'xhtml';
     114
     115            /**
     116             * Filters the HTML format of widgets with navigation links.
     117             *
     118             * @since 5.5.0
     119             *
     120             * @param string $format The type of markup to use in widgets with navigation links.
     121             *                       Accepts 'html5', 'xhtml'.
     122             */
     123            $format = apply_filters( 'navigation_widgets_format', $format );
     124
     125            if ( 'html5' === $format ) {
     126                // The title may be filtered: Strip out HTML and make sure the aria-label is never empty.
     127                $title      = trim( strip_tags( $title ) );
     128                $aria_label = $title ? $title : $default_title;
     129                echo '<nav role="navigation" aria-label="' . esc_attr( $aria_label ) . '">';
     130            }
    112131            ?>
    113         <ul>
    114             <?php
    115             $cat_args['title_li'] = '';
    116 
    117             /**
    118              * Filters the arguments for the Categories widget.
    119              *
    120              * @since 2.8.0
    121              * @since 4.9.0 Added the `$instance` parameter.
    122              *
    123              * @param array $cat_args An array of Categories widget options.
    124              * @param array $instance Array of settings for the current widget.
    125              */
    126             wp_list_categories( apply_filters( 'widget_categories_args', $cat_args, $instance ) );
    127             ?>
    128         </ul>
     132
     133            <ul>
     134                <?php
     135                $cat_args['title_li'] = '';
     136
     137                /**
     138                 * Filters the arguments for the Categories widget.
     139                 *
     140                 * @since 2.8.0
     141                 * @since 4.9.0 Added the `$instance` parameter.
     142                 *
     143                 * @param array $cat_args An array of Categories widget options.
     144                 * @param array $instance Array of settings for the current widget.
     145                 */
     146                wp_list_categories( apply_filters( 'widget_categories_args', $cat_args, $instance ) );
     147                ?>
     148            </ul>
     149
     150            <?php if ( 'html5' === $format ) : ?>
     151                </nav>
     152            <?php endif; ?>
     153
    129154            <?php
    130155        }
Note: See TracChangeset for help on using the changeset viewer.