Make WordPress Core


Ignore:
Timestamp:
01/11/2026 06:34:57 AM (6 months ago)
Author:
westonruter
Message:

Themes: Use WP_HTML_Tag_Processor to insert the block template skip link instead of JavaScript.

  • The skip link now works when JavaScript is turned off.
  • By removing the script, the amount of JavaScript sent to the client is reduced for a very marginal performance improvement.
  • A new wp-block-template-skip-link stylesheet is registered, with minification and path data for inlining.
  • The CSS for the skip link now has an RTL version generated, although it is not yet served when the styles are inlined. See #61625.
  • The wp_enqueue_block_template_skip_link() function now exclusively enqueues the stylesheet since the script is removed.
  • For backwards-compatibility, the skip link will continue to be omitted if the_block_template_skip_link() is unhooked from the wp_footer action or wp_enqueue_block_template_skip_link() is unhooked from wp_enqueue_scripts.

Developed in https://github.com/WordPress/wordpress-develop/pull/10676

Follow-up to [56932], [51003].

Props rutviksavsani, westonruter, dmsnell, whiteshadow01, Slieptsov.
See #59505, #53176.
Fixes #64361.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/theme-templates.php

    r61178 r61469  
    100100
    101101/**
    102  * Enqueues the skip-link script & styles.
     102 * Enqueues the skip-link styles.
    103103 *
    104104 * @access private
    105105 * @since 6.4.0
     106 * @since 7.0.0 A script is no longer printed in favor of being added via {@see _block_template_add_skip_link()}.
    106107 *
    107108 * @global string $_wp_current_template_content
     
    126127        }
    127128
    128         $skip_link_styles = '
    129                 .skip-link.screen-reader-text {
    130                         border: 0;
    131                         clip-path: inset(50%);
    132                         height: 1px;
    133                         margin: -1px;
    134                         overflow: hidden;
    135                         padding: 0;
    136                         position: absolute !important;
    137                         width: 1px;
    138                         word-wrap: normal !important;
    139                 }
    140 
    141                 .skip-link.screen-reader-text:focus {
    142                         background-color: #eee;
    143                         clip-path: none;
    144                         color: #444;
    145                         display: block;
    146                         font-size: 1em;
    147                         height: auto;
    148                         left: 5px;
    149                         line-height: normal;
    150                         padding: 15px 23px 14px;
    151                         text-decoration: none;
    152                         top: 5px;
    153                         width: auto;
    154                         z-index: 100000;
    155                 }';
    156 
    157         $handle = 'wp-block-template-skip-link';
    158 
    159         /**
    160          * Print the skip-link styles.
    161          */
    162         wp_register_style( $handle, false );
    163         wp_add_inline_style( $handle, $skip_link_styles );
    164         wp_enqueue_style( $handle );
    165 
    166         /**
    167          * Enqueue the skip-link script.
    168          */
    169         ob_start();
    170         ?>
    171         <script>
    172         ( function() {
    173                 var skipLinkTarget = document.querySelector( 'main' ),
    174                         sibling,
    175                         skipLinkTargetID,
    176                         skipLink;
    177 
    178                 // Early exit if a skip-link target can't be located.
    179                 if ( ! skipLinkTarget ) {
    180                         return;
    181                 }
    182 
    183                 /*
    184                  * Get the site wrapper.
    185                  * The skip-link will be injected in the beginning of it.
    186                  */
    187                 sibling = document.querySelector( '.wp-site-blocks' );
    188 
    189                 // Early exit if the root element was not found.
    190                 if ( ! sibling ) {
    191                         return;
    192                 }
    193 
    194                 // Get the skip-link target's ID, and generate one if it doesn't exist.
    195                 skipLinkTargetID = skipLinkTarget.id;
    196                 if ( ! skipLinkTargetID ) {
    197                         skipLinkTargetID = 'wp--skip-link--target';
    198                         skipLinkTarget.id = skipLinkTargetID;
    199                 }
    200 
    201                 // Create the skip link.
    202                 skipLink = document.createElement( 'a' );
    203                 skipLink.classList.add( 'skip-link', 'screen-reader-text' );
    204                 skipLink.id = 'wp-skip-link';
    205                 skipLink.href = '#' + skipLinkTargetID;
    206                 skipLink.innerText = '<?php /* translators: Hidden accessibility text. Do not use HTML entities (&nbsp;, etc.). */ esc_html_e( 'Skip to content' ); ?>';
    207 
    208                 // Inject the skip link.
    209                 sibling.parentElement.insertBefore( skipLink, sibling );
    210         }() );
    211         </script>
    212         <?php
    213         $skip_link_script = wp_remove_surrounding_empty_script_tags( ob_get_clean() );
    214         $script_handle    = 'wp-block-template-skip-link';
    215         wp_register_script( $script_handle, false, array(), false, array( 'in_footer' => true ) );
    216         wp_add_inline_script( $script_handle, $skip_link_script );
    217         wp_enqueue_script( $script_handle );
     129        wp_enqueue_style( 'wp-block-template-skip-link' );
    218130}
    219131
Note: See TracChangeset for help on using the changeset viewer.