SEO PHP Beginner

Add Rules to the Virtual robots.txt

Add your own rules to the robots.txt file WordPress generates, keep the defaults, and point crawlers to the right sitemap.

Tested on WordPress 7.1 PHP 8.3 No plugin needed Updated
functions.php 21 lines
add_filter( 'robots_txt', function ( string $output, bool $public ): string {
	// Respect Settings > Reading > "Discourage search engines".
	if ( ! $public ) {
		return $output;
	}

	// Rules added to WordPress's own "User-agent: *" group.
	$output .= "Disallow: /?s=\n";
	$output .= "Disallow: /search/\n";

	// Example: block an AI training crawler completely. Remove if you don't want this.
	$output .= "\nUser-agent: GPTBot\nDisallow: /\n";

	// WordPress only adds its sitemap line when core sitemaps are on. Add it if missing.
	$sitemap = get_sitemap_url( 'index' );
	if ( $sitemap && ! str_contains( $output, 'Sitemap:' ) ) {
		$output .= "\nSitemap: " . esc_url_raw( $sitemap ) . "\n";
	}

	return $output;
}, 10, 2 );

Where to add this

Recommended A code snippets plugin Survives theme changes and is easy to switch off.
Child theme At the end of your child theme's functions.php file.
Must-use plugin As a .php file in wp-content/mu-plugins, starting with <?php.

Always add PHP to a staging site first, or have FTP or file manager access ready, so you can undo it if something goes wrong.

WordPress builds a virtual robots.txt file on the fly. By default it blocks /wp-admin/, allows admin-ajax.php and lists the core sitemap. This snippet adds your own rules to that file instead of replacing it, so you keep the sensible defaults.

How it works

The robots_txt filter receives the generated text and whether the site is public. If “Discourage search engines” is ticked, the snippet leaves everything alone. Otherwise it appends lines to WordPress’s own User-agent: * group, adds an example group that blocks OpenAI’s GPTBot, and makes sure there’s a Sitemap: line pointing to the address get_sitemap_url() returns.

What changed from the old version

  • It no longer blocks /wp-includes/. Google needs the CSS and JavaScript in that folder to render your pages, and blocking it can hurt how your pages are understood.
  • It no longer blocks /page/. Paginated archives are how crawlers reach older posts.
  • The sitemap line now uses WordPress’s real sitemap at /wp-sitemap.xml, or whatever your SEO plugin registers, instead of a guessed /sitemap.xml.

Things to watch

This only works when there is no physical robots.txt file in your site’s root folder. If one exists, the web server sends it directly and WordPress never runs. robots.txt is also a request, not a lock: well-behaved crawlers follow it, bad ones ignore it.

How to test it

Open yoursite.com/robots.txt. You should see WordPress’s default lines, your additions and one Sitemap: line.

This website uses cookies to enhance your browsing experience and ensure the site functions properly. By continuing to use this site, you acknowledge and accept our use of cookies.

Accept All Accept Required Only