wp_robots() WordPress Function
The wp_robots function is used to generate and return the robots meta tag for a given Wordpress blog. This function can be used to control whether search engines should index a given Wordpress site, and whether they should follow the links on that site. The wp_robots function accepts two parameters: $public and $robots. The $public parameter is a boolean value that indicates whether the current Wordpress site is public (true) or private (false). The $robots parameter is an array of key/value pairs that control the various directives that will be included in the robots meta tag.
wp_robots() #
Displays the robots meta tag as necessary.
Description
Gathers robots directives to include for the current context, using the ‘wp_robots’ filter. The directives are then sanitized, and the robots meta tag is output if there is at least one relevant directive.
Source
File: wp-includes/robots-template.php
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";
}
Expand full source codeCollapse full source codeView on TracView on GitHub
Changelog
| Version | Description |
|---|---|
| 5.7.1 | No longer prevents specific directives to occur together. |
| 5.7.0 | Introduced. |