do_robotstxtaction-hookWP 2.1.0

Allows you to completely replace the content of the /robots.txt page.

Fires while the virtual robots.txt response is generated, before the standard content is added. The hook allows custom lines to be output at the beginning of the response.

When /robots.txt is requested and no physical file exists in the site root, WordPress serves a “virtual” file. This hook is called while it is generated. Everything output with echo is placed at the beginning of the response before the standard block.

Use robots_txt when the default robots.txt content must be extended.

Notes:

  • The event passes no parameters.

  • Output through echo; there is no return value.

  • Do not output headers — WordPress sends them itself.

  • End every directive with a newline \n.

  • If a physical robots.txt exists in the root, the web server serves it and the event does not fire.

Usage

add_action( 'do_robotstxt', 'wp_kama_do_robotstxt_action' );

/**
 * Function for `do_robotstxt` action-hook.
 * 
 * @return void
 */
function wp_kama_do_robotstxt_action(){

	// action...
}

Examples

#1 Replace the content of the /robots.txt page

add_action( 'do_robotstxt', 'wp_kama_robots_txt' );

function wp_kama_robots_txt(){
	$lines = [
		'User-agent: *',
		'Disallow: /wp-admin/',
		'Disallow: /wp-includes/',
		'',
	];

	echo implode( "\r\n", $lines );

	die; // Stop PHP execution
}

Now visiting http://site.com/robots.txt shows:

User-agent: *
Disallow: /wp-admin/
Disallow: /wp-includes/

#2 Block a specific bot

Add a section for a “bad” bot that prohibits crawling the entire site.

add_action( 'do_robotstxt', 'wpk_block_badbot_in_robots' );

function wpk_block_badbot_in_robots() {
	echo "User-agent: BadBot\n";
	echo "Disallow: /\n\n";
}

Changelog

Since 2.1.0 Introduced.

Where the hook is called

do_robots()
do_robotstxt
wp-includes/functions.php 1723
do_action( 'do_robotstxt' );

Where the hook is used in WordPress

Usage not found.