Make WordPress Core


Ignore:
Timestamp:
02/26/2026 02:34:53 AM (9 months ago)
Author:
joedolson
Message:

Users: A11y: Improve title attributes in author link functions.

Remove the title attribute from the_author_posts_link() and related functions, retaining text for use in the_author_posts_link filter, and add parameter to disable title attributes in the_author_link().

Default behavior will still differentiate the two links, but adds the option to remove all title attributes.

Props sabernhardt, alh0319, adnanlimdi, audrasjb, joedolson.
Fixes #62835. See #26559.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/author-template.php

    r61454 r61745  
    230230 *
    231231 * @since 3.0.0
     232 * @since 7.0.0 Added `$use_title_attr` parameter.
    232233 *
    233234 * @global WP_User $authordata The current author's data.
    234235 *
     236 * @param bool $use_title_attr Optional. Whether to add a title attribute.
     237 *                             Default true.
    235238 * @return string An HTML link if the author's URL exists in user meta,
    236239 *                otherwise the result of get_the_author().
    237240 */
    238 function get_the_author_link() {
     241function get_the_author_link( $use_title_attr = true ) {
    239242        if ( get_the_author_meta( 'url' ) ) {
    240243                global $authordata;
    … …  
    243246                $author_display_name = get_the_author();
    244247
     248                /* translators: %s: Author's display name. */
     249                $author_title = sprintf( __( 'Visit %s’s website' ), $author_display_name );
     250
    245251                $link = sprintf(
    246                         '<a href="%1$s" title="%2$s" rel="author external">%3$s</a>',
     252                        '<a href="%1$s"%2$s rel="author external">%3$s</a>',
    247253                        esc_url( $author_url ),
    248                         /* translators: %s: Author's display name. */
    249                         esc_attr( sprintf( __( 'Visit %s&#8217;s website' ), $author_display_name ) ),
     254                        $use_title_attr ? ' title="' . esc_attr( $author_title ) . '"' : '',
    250255                        $author_display_name
    251256                );
    … …  
    275280 *
    276281 * @since 2.1.0
    277  */
    278 function the_author_link() {
    279         echo get_the_author_link();
     282 * @since 7.0.0 Added `$use_title_attr` parameter.
     283 *
     284 * @param bool $use_title_attr Optional. Whether to add a title attribute.
     285 *                             Default true.
     286 */
     287function the_author_link( $use_title_attr = true ) {
     288        echo get_the_author_link( $use_title_attr );
    280289}
    281290
    … …  
    311320 *
    312321 * @since 4.4.0
     322 * @since 7.0.0 Removed title attribute.
    313323 *
    314324 * @global WP_User $authordata The current author's data.
    … …  
    323333        }
    324334
     335        $author = get_the_author();
     336        /* translators: %s: Author's display name. */
     337        $title  = sprintf( __( 'Posts by %s' ), $author );
     338
    325339        $link = sprintf(
    326                 '<a href="%1$s" title="%2$s" rel="author">%3$s</a>',
     340                '<a href="%1$s" rel="author">%2$s</a>',
    327341                esc_url( get_author_posts_url( $authordata->ID, $authordata->user_nicename ) ),
    328                 /* translators: %s: Author's display name. */
    329                 esc_attr( sprintf( __( 'Posts by %s' ), get_the_author() ) ),
    330                 get_the_author()
     342                $author
    331343        );
    332344
    … …  
    335347         *
    336348         * @since 2.9.0
    337          *
    338          * @param string $link HTML link.
    339          */
    340         return apply_filters( 'the_author_posts_link', $link );
     349         * @since 7.0.0 Added `$author` and `$title` parameters.
     350         *
     351         * @param string $link   HTML link.
     352         * @param string $author Author's display name.
     353         * @param string $title  Text originally used for a title attribute.
     354         */
     355        return apply_filters( 'the_author_posts_link', $link, $author, $title );
    341356}
    342357
    … …  
    537552
    538553                $link = sprintf(
    539                         '<a href="%1$s" title="%2$s">%3$s</a>',
     554                        '<a href="%1$s">%2$s</a>',
    540555                        esc_url( get_author_posts_url( $author->ID, $author->user_nicename ) ),
    541                         /* translators: %s: Author's display name. */
    542                         esc_attr( sprintf( __( 'Posts by %s' ), $author->display_name ) ),
    543556                        $name
    544557                );
Note: See TracChangeset for help on using the changeset viewer.