the_block_template_skip_link()
概述
the_block_template_skip_link() 是一个已弃用的 WordPress 函数,用于输出块模板的跳过链接脚本和样式。它已被 wp_enqueue_block_template_skip_link() 替代。
关键要点
- 该函数在 WordPress 6.4.0 版本中被弃用,建议使用 wp_enqueue_block_template_skip_link() 替代。
- 函数仅在块主题和块模板环境下执行,通过 current_theme_supports('block-templates') 和 $_wp_current_template_content 检查条件。
- 使用 _deprecated_function() 标记为弃用,以提醒开发者更新代码。
注意事项
- 从 WordPress 6.4.0 开始,应避免使用此函数,改用 wp_enqueue_block_template_skip_link() 以确保兼容性和最佳实践。
- 函数依赖于全局变量 $_wp_current_template_content,需确保在块模板上下文中调用。
Prints the skip-link script & styles.
Source
function the_block_template_skip_link() {
_deprecated_function( __FUNCTION__, '6.4.0', 'wp_enqueue_block_template_skip_link()' );
global $_wp_current_template_content;
// Early exit if not a block theme.
if ( ! current_theme_supports( 'block-templates' ) ) {
return;
}
// Early exit if not a block template.
if ( ! $_wp_current_template_content ) {
return;
}
?>
<style id="skip-link-styles">
.skip-link.screen-reader-text {
border: 0;
clip-path: inset(50%);
height: 1px;
margin: -1px;
overflow: hidden;
padding: 0;
position: absolute !important;
width: 1px;
word-wrap: normal !important;
}
.skip-link.screen-reader-text:focus {
background-color: #eee;
clip-path: none;
color: #444;
display: block;
font-size: 1em;
height: auto;
left: 5px;
line-height: normal;
padding: 15px 23px 14px;
text-decoration: none;
top: 5px;
width: auto;
z-index: 100000;
}
</style>
<script>
( function() {
var skipLinkTarget = document.querySelector( 'main' ),
sibling,
skipLinkTargetID,
skipLink;
// Early exit if a skip-link target can't be located.
if ( ! skipLinkTarget ) {
return;
}
/*
* Get the site wrapper.
* The skip-link will be injected in the beginning of it.
*/
sibling = document.querySelector( '.wp-site-blocks' );
// Early exit if the root element was not found.
if ( ! sibling ) {
return;
}
// Get the skip-link target's ID, and generate one if it doesn't exist.
skipLinkTargetID = skipLinkTarget.id;
if ( ! skipLinkTargetID ) {
skipLinkTargetID = 'wp--skip-link--target';
skipLinkTarget.id = skipLinkTargetID;
}
// Create the skip link.
skipLink = document.createElement( 'a' );
skipLink.classList.add( 'skip-link', 'screen-reader-text' );
skipLink.href = '#' + skipLinkTargetID;
skipLink.innerHTML = '<?php /* translators: Hidden accessibility text. */ esc_html_e( 'Skip to content' ); ?>';
// Inject the skip link.
sibling.parentElement.insertBefore( skipLink, sibling );
}() );
</script>
</pre><p class="wporg-dot-link-list"><a href="https://developer.wordpress.org/reference/files/wp-includes/deprecated.php/">View all references</a> <a href="https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-includes/deprecated.php#L6148">View on Trac</a> <a href="https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/deprecated.php#L6148-L6244">View on GitHub</a></p></section>
<section class="wp-block-wporg-code-reference-related" data-nosnippet="true"><h2 id="related" class="is-toc-heading wp-block-heading has-heading-5-font-size" tabindex="-1" ><a href="#related">Related</a></h2> <section style="margin-top:var(--wp--preset--spacing--20)" class="wp-block-wporg-code-table" id="uses"><figure class="wp-block-table "><table><thead><tr><th scope="col">Uses</th><th scope="col">Description</th></tr></thead><tbody><tr class=""><td><a href="https://developer.wordpress.org/reference/functions/esc_html_e/">esc_html_e()</a><code>wp-includes/l10n.php
Displays translated text that has been escaped for safe use in HTML output.
current_theme_supports()wp-includes/theme.php
Checks a theme’s support for a given feature.
_deprecated_function()wp-includes/functions.php
Marks a function as deprecated and inform when it has been used.
Changelog
| Version | Description |
|---|---|
| 6.4.0 | Deprecated. Use wp_enqueue_block_template_skip_link() instead. |
| 5.8.0 | Introduced. |