wp_robots_noindex()
云策文档标注
概述
wp_robots_noindex() 是一个 WordPress 函数,用于根据站点配置向 robots 元标签添加 noindex 指令。它通常作为 'wp_robots' 过滤器的回调函数使用,以控制搜索引擎索引行为。
关键要点
- 函数作用:当站点设置为非公开时,向 robots 元标签添加 noindex,阻止搜索引擎索引页面内容。
- 使用方式:通过 add_filter('wp_robots', 'wp_robots_noindex') 添加到 'wp_robots' 过滤器。
- 参数:接受一个关联数组 $robots,表示 robots 指令。
- 返回值:返回过滤后的 robots 指令数组。
- 依赖函数:内部调用 wp_robots_no_robots() 和 get_option('blog_public') 来检查站点公开状态。
- 版本历史:自 WordPress 5.7.0 版本引入。
代码示例
function wp_robots_noindex( array $robots ) {
if ( ! get_option( 'blog_public' ) ) {
return wp_robots_no_robots( $robots );
}
return $robots;
}
原文内容
Adds noindex to the robots meta tag if required by the site configuration.
Description
If a blog is marked as not being public then noindex will be output to tell web robots not to index the page content. Add this to the ‘wp_robots’ filter.
Typical usage is as a ‘wp_robots’ callback:
add_filter( 'wp_robots', 'wp_robots_noindex' );
See also
Parameters
$robotsarrayrequired-
Associative array of robots directives.
Source
function wp_robots_noindex( array $robots ) {
if ( ! get_option( 'blog_public' ) ) {
return wp_robots_no_robots( $robots );
}
return $robots;
}
Changelog
| Version | Description |
|---|---|
| 5.7.0 | Introduced. |