wp_robots
Allows you to add or remove robots meta tag directives.
The robots meta tag is output in the HEAD section of the page. For example:
<meta name='robots' content='directive, directive'> <!-- for example --> <meta name='robots' content='noindex, follow, max-image-preview:large' />
If the filter produces an empty array (the function returns an empty array), the robots meta tag is not output.
See also: Robots API in WordPress.
See the available robots meta tag directives:
Usage
add_filter( 'wp_robots', 'wp_kama_robots_filter' );
/**
* Function for `wp_robots` filter-hook.
*
* @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.
*
* @return array
*/
function wp_kama_robots_filter( $robots ){
// filter...
return $robots;
}
- $robots(array)
- Associative array of
[ directive => value ]pairs. The key contains the directive name. The value can be astringfor a directive with a value, ortruefor a boolean directive without a value.
Examples
#1 Prevent various site pages from being indexed
add_filter( 'wp_robots', 'wp_kama_robots_tag' );
function wp_kama_robots_tag( $robots ){
// Prevent attachment, pagination, and post type archive pages from being indexed.
if(
is_attachment() ||
is_paged() ||
is_post_type_archive()
){
$robots['noindex'] = true;
}
// Prevent taxonomies from being indexed when their terms have no description.
if(
( is_category() || is_tag() || is_tax() )
&& ! trim( get_queried_object()->description )
){
$robots['noindex'] = true;
}
// Prevent pages with the ?filter query parameter from being indexed.
if( isset( $_GET['filter'] ) )
$robots['none'] = true;
return $robots;
}
#2 Remove existing directives
Suppose a plugin prevents pages from being indexed even though they should be indexed. This corrects the behavior:
add_filter( 'wp_robots', 'wp_kama_robots_tag' );
function wp_kama_robots_tag( $robots ){
// Remove the unwanted directive.
unset( $robots['noindex'] );
return $robots;
}Changelog
| Since 5.7.0 | Introduced. |
Where the hook is called
wp_robots
wp-includes/robots-template.php 32
$robots = apply_filters( 'wp_robots', array() );
Where the hook is used in WordPress
wp-activate.php 116
add_filter( 'wp_robots', 'wp_robots_sensitive_page' );
wp-includes/class-wp-customize-manager.php 1922
add_filter( 'wp_robots', 'wp_robots_no_robots' );
wp-includes/default-filters.php 277
add_filter( 'wp_robots', 'wp_robots_noindex' );
wp-includes/default-filters.php 278
add_filter( 'wp_robots', 'wp_robots_noindex_embeds' );
wp-includes/default-filters.php 279
add_filter( 'wp_robots', 'wp_robots_noindex_search' );
wp-includes/default-filters.php 280
add_filter( 'wp_robots', 'wp_robots_max_image_preview_large' );
wp-includes/default-filters.php 388
add_filter( 'wp_robots', 'wp_robots_no_robots' );
wp-includes/functions.php 3968
add_filter( 'wp_robots', 'wp_robots_no_robots' );
wp-includes/functions.php 3970
remove_filter( 'wp_robots', 'wp_robots_noindex_embeds' );
wp-includes/functions.php 3971
remove_filter( 'wp_robots', 'wp_robots_noindex_search' );
wp-login.php 49
add_filter( 'wp_robots', 'wp_robots_sensitive_page' );
wp-signup.php 13
add_filter( 'wp_robots', 'wp_robots_no_robots' );