noindex()
云策文档标注
概述
noindex() 函数用于根据博客配置输出 noindex meta 标签,以阻止搜索引擎索引页面内容。该函数已弃用,建议使用 wp_robots_noindex() 替代。
关键要点
- noindex() 函数检查博客是否设置为非公开(blog_public 选项为 '0'),如果是,则调用 wp_no_robots() 输出 noindex meta 标签。
- 该函数自 WordPress 5.7.0 起已弃用,推荐使用 wp_robots_noindex() 函数,并通过 'wp_robots' 过滤器实现类似功能。
- 典型用法是作为 'wp_head' 钩子的回调函数,例如:add_action( 'wp_head', 'noindex' )。
- 相关函数包括 wp_no_robots()、_deprecated_function() 和 get_option(),用于处理弃用通知和获取选项值。
代码示例
function noindex() {
_deprecated_function( __FUNCTION__, '5.7.0', 'wp_robots_noindex()' );
// If the blog is not public, tell robots to go away.
if ( '0' == get_option( 'blog_public' ) ) {
wp_no_robots();
}
}注意事项
- noindex() 函数已弃用,开发者应避免在新代码中使用,并迁移到 wp_robots_noindex() 以保持兼容性和最佳实践。
- 该函数仅当博客配置为非公开时才会输出 noindex 标签,适用于需要控制搜索引擎索引的场景。
原文内容
Displays a noindex meta tag if required by the blog configuration.
Description
If a blog is marked as not being public then the noindex meta tag will be output to tell web robots not to index the page content.
Typical usage is as a ‘wp_head’ callback:
add_action( 'wp_head', 'noindex' );
See also
Source
function noindex() {
_deprecated_function( __FUNCTION__, '5.7.0', 'wp_robots_noindex()' );
// If the blog is not public, tell robots to go away.
if ( '0' == get_option( 'blog_public' ) ) {
wp_no_robots();
}
}
Changelog
| Version | Description |
|---|---|
| 5.7.0 | Deprecated. Use wp_robots_noindex() instead on 'wp_robots' filter. |
| 2.1.0 | Introduced. |