函数文档

wp_robots_noindex_search()

💡 云策文档标注

概述

wp_robots_noindex_search() 是一个 WordPress 函数,用于在搜索页面时向 robots 元标签添加 noindex 指令,以阻止搜索引擎索引该页面内容。它通常作为 'wp_robots' 过滤器的回调函数使用。

关键要点

  • 函数作用:当执行搜索查询时,自动添加 noindex 到 robots 元标签。
  • 使用方式:通过 add_filter() 添加到 'wp_robots' 过滤器,例如 add_filter( 'wp_robots', 'wp_robots_noindex_search' )。
  • 参数:接受一个关联数组 $robots,表示 robots 指令。
  • 返回值:返回过滤后的 robots 指令数组。
  • 相关函数:与 wp_robots_no_robots() 和 is_search() 配合使用。
  • 版本历史:自 WordPress 5.7.0 版本引入。

代码示例

function wp_robots_noindex_search( array $robots ) {
	if ( is_search() ) {
		return wp_robots_no_robots( $robots );
	}

	return $robots;
}

📄 原文内容

Adds noindex to the robots meta tag if a search is being performed.

Description

If a search is being performed 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_search' );

See also

Parameters

$robotsarrayrequired
Associative array of robots directives.

Return

array Filtered robots directives.

Source

function wp_robots_noindex_search( array $robots ) {
	if ( is_search() ) {
		return wp_robots_no_robots( $robots );
	}

	return $robots;
}

Changelog

Version Description
5.7.0 Introduced.