apply_filters( ‘wp_robots’, array $robots )

Filters the directives to be included in the ‘robots’ meta tag.

Description

The meta tag will only be included as necessary.

Parameters

$robotsarray
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.

Source

$robots = apply_filters( 'wp_robots', array() );

Changelog

VersionDescription
5.7.0Introduced.

User Contributed Notes

  1. Skip to note 2 content

    Example of adding a noindex robots tag.

    add_filter( 'wp_robots', function( $robots ) {
    	$robots['noindex'] = true;
    
    	return $robots;
    } );

    To affect pages with a certain page template, you can add this directly to the template, as long as it’s above the call to wp_header().

    To have more granular control over what pages are affected, you can add the snippet to your functions.php file and work with the current page inside the function:

    add_filter( 'wp_robots', function( $robots ) {
    	$id = get_the_ID();
    
    	if ( 19 === $id ) {
    		$robots['noindex'] = true;
    	}
    
    	return $robots;
    } );

You must log in before being able to contribute a note or feedback.