is_robots()WP 2.1.0

Checks if this is a request to robots.txt file. A conditional tag.

If there is no robots.txt file in the site root directory, WordPress generates the content of this file on the fly, and this conditional tag return true.

For such request (to robots.txt that doesn't physically exist) WordPress trigger an action:

do_action( 'do_robots' );

This function is a wrapper for this code:

global $wp_query;
$wp_query->is_robots();
1 time — 0.000014 sec (very fast) | 50000 times — 0.02 sec (speed of light) | PHP 7.1.2, WP 4.7.3

No Hooks.

Return

true|false. Whether the query is for the robots.txt file.

Usage

if( is_robots() ){
	// this is robots.txt file
}

Examples

0

#1 Check the current robots.txt request

You can check the request after it has been processed - it can be done on wp hook. Suppose we need to do something on wp hook, but we don't want to do anything if it is a request to the robots.txt file:

do_action( 'wp', function(){

	if( is_robots() ){
		return;
	}

	// do anything
} );

Notes

  • Global. WP_Query. $wp_query WordPress Query object.

Changelog

Since 2.1.0 Introduced.

is_robots() code WP 6.4.3

function is_robots() {
	global $wp_query;

	if ( ! isset( $wp_query ) ) {
		_doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
		return false;
	}

	return $wp_query->is_robots();
}