WordPress at a glance

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.000014s = very fast | 50000 times = 0.02s = speed of light | PHP 7.1.2, WP 4.7.3

No Hooks.

Return

true/false.

Usage

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

Notes

  • Global. WP_Query. $wp_query Global WP_Query instance.

Changelog

Since 2.1.0 Introduced.

Code of is robots: wp-includes/query.php WP 5.2.2

<?php
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();
}

Related Functions

From tag: Conditional tags (page type and request)

More from tag: Conditional tags (all)

More from category: Main Query

No comments
    Hello, !     Log In . Register