wp_robots()WP 5.7.0

Выводит на экран мета-тег robots.

Объединяет все имеющиеся директивы, очищает их и выводит в мета-теге robots:

<meta name='robots' content='директива, директива'>

<!-- например -->
<meta name='robots' content='noindex, follow, max-image-preview:large' />

Читайте также: Robots API в WodPress.

Функция автоматически вызывается ядром WordPress на событии wp_head, чтобы мета-тег robots выводился автоматически где это необходимо. Делается это таким хуком в ядре:

add_action( 'wp_head', 'wp_robots', 1 );

Поэтому плагин или тема не должны вызывать эту функцию отдельно. В особых случаях, например, когда шаблон не использует wp_head(), новая функция может быть подключена к своему фильтру или вызвана напрямую в коде:

// подключение к фильтру
add_action( 'my_custom_template_head', 'wp_robots' );

// прямой вызов
<?php wp_robots() ?>
Хуки из функции

Возвращает

null. Выводит HTML код на экран.

Использование

wp_robots();

Примеры

0

#1 Пример добавления произвольный директивы в метатег robots

Этот пример показывает как добавить директиву follow к имеющимся директивам метатега robots.

Чтобы добавить свои директивы нужно использовать хук wp_robots. Он принимает массив в где в ключе нужно указать название директивы, а в значении её значение.

add_filter( 'wp_robots', 'my_wp_robots_add_follow' );

function my_wp_robots_add_follow( $robots ) {
	$robots['follow'] = true;

	return $robots;
}

Список изменений

С версии 5.7.0 Введена.
С версии 5.7.1 No longer prevents specific directives to occur together.

Код wp_robots() WP 6.5.2

function wp_robots() {
	/**
	 * Filters the directives to be included in the 'robots' meta tag.
	 *
	 * The meta tag will only be included as necessary.
	 *
	 * @since 5.7.0
	 *
	 * @param array $robots Associative array of directives. Every key must be the name of the directive, and the
	 *                      corresponding value must either be a string to provide as value for the directive or a
	 *                      boolean `true` if it is a boolean directive, i.e. without a value.
	 */
	$robots = apply_filters( 'wp_robots', array() );

	$robots_strings = array();
	foreach ( $robots as $directive => $value ) {
		if ( is_string( $value ) ) {
			// If a string value, include it as value for the directive.
			$robots_strings[] = "{$directive}:{$value}";
		} elseif ( $value ) {
			// Otherwise, include the directive if it is truthy.
			$robots_strings[] = $directive;
		}
	}

	if ( empty( $robots_strings ) ) {
		return;
	}

	echo "<meta name='robots' content='" . esc_attr( implode( ', ', $robots_strings ) ) . "' />\n";
}