wppaste
WordPress

_block_template_add_skip_link( string $template_html ): string

Since
7.0.0
Source
wp-includes/block-template.php:350
Inserts the block template skip-link into the template HTML.

Description

When a MAIN element exists in the template, this function will ensure that the element contains an id attribute, and it will insert a link to that MAIN element before the first DIV.wp-site-blocks element, which is the wrapper for all blocks in a block template as constructed by get_the_block_template_html().

Example:

// Input.
<div class="wp-site-blocks">
 <nav>...</nav>
 <main>
 <h2>...

// Output.
<a href="#wp--skip-link--target" id="wp-skip-link" class="...">
<div class="wp-site-blocks">
 <nav>...</nav>
 <main id="wp--skip-link--target">
 <h2>...

When the MAIN element already contains a non-empty id value it will be used instead of the default skip-link id.

Compatibility

WordPress
since 7.0.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in 2 of the 5 tracked releases, added in 7.0.0, and compiles on PHP 7.4 through 8.6-dev.

Parameters

$template_htmlstring
Block template markup.

Return value

string
Modified markup with skip link when applicable.

Performance profile

How much work a call to _block_template_add_skip_link() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Trivial

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
12–52

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 56.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
1

1 place in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()one call below _block_template_add_skip_link()

Further down the call graph this can also reach option, cache, serialize, query and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 4 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost _block_template_add_skip_link() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
!->next_tag()12->next_tag()
always19->next_tag(), ->set_bookmark(), ->next_tag()
->next_tag() && is_string($skip_link_target_id) && $skip_link_target_id !== ""47->next_tag(), ->set_bookmark(), ->next_tag(), ->get_attribute(), ->seek(), esc_url(), esc_html__(), ->insert_before(), ->get_updated_html()
->next_tag()50–52->next_tag(), ->set_bookmark(), ->next_tag(), ->get_attribute(), ->set_attribute(), ->seek(), esc_url(), esc_html__(), ->insert_before(), ->get_updated_html()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev5612–524
8.55612–524
8.45612–5241 fewer instruction than PHP 8.3
8.35712–534
8.25712–534
8.15712–534
7.45712–534

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Uses · 3

  • esc_url()Checks and cleans a URL.
  • esc_html__()Retrieves the translation of $text and escapes it for safe use in HTML output.
  • class@anonymous::__construct()

Used by · 1

Source code

function _block_template_add_skip_link( string $template_html ): string {	// Anonymous subclass of WP_HTML_Tag_Processor to access protected bookmark spans.	$processor = new class( $template_html ) extends WP_HTML_Tag_Processor {		/**		 * Inserts text before the current token.		 *		 * @param string $text Text to insert.		 */		public function insert_before( string $text ) {			$this->set_bookmark( 'here' );			$this->lexical_updates[] = new WP_HTML_Text_Replacement( $this->bookmarks['here']->start, 0, $text );		}	}; 	// Find and bookmark the first DIV.wp-site-blocks.	if (		! $processor->next_tag(			array(				'tag_name'   => 'DIV',				'class_name' => 'wp-site-blocks',			)		)	) {		return $template_html;	}	$processor->set_bookmark( 'skip_link_insertion_point' ); 	// Ensure the MAIN element has an ID.	if ( ! $processor->next_tag( 'MAIN' ) ) {		return $template_html;	} 	$skip_link_target_id = $processor->get_attribute( 'id' );	if ( ! is_string( $skip_link_target_id ) || '' === $skip_link_target_id ) {		$skip_link_target_id = 'wp--skip-link--target';		$processor->set_attribute( 'id', $skip_link_target_id );	} 	// Seek back to the bookmarked insertion point.	$processor->seek( 'skip_link_insertion_point' ); 	$skip_link = sprintf(		'<a class="skip-link screen-reader-text" id="wp-skip-link" href="%s">%s</a>',		esc_url( '#' . $skip_link_target_id ),		/* translators: Hidden accessibility text. */		esc_html__( 'Skip to content' )	);	$processor->insert_before( $skip_link ); 	return $processor->get_updated_html();}

Changelog

Introduced in 7.0.0. Unchanged from 7.0.4 through 7.1.0.

  1. 7.0.4
  2. 7.1.0

Signature, return type and hooks compared across 2 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/block-template.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.