Customize robots.txt Output
Overrides the virtual WordPress robots.txt via filter to add custom Disallow rules, a Sitemap declaration, and crawl-delay directives.
PHPby SnipCraft
php
<?php
if ( ! function_exists( 'scseed_custom_robots_txt' ) ) {
function scseed_custom_robots_txt( $output, $public ) {
if ( '0' === $public ) {
// Site is set to discourage search engines — leave robots.txt as-is.
return $output;
}
$sitemap_url = esc_url( home_url( '/wp-sitemap.xml' ) );
$output = "User-agent: *
";
$output .= "Disallow: /wp-admin/
";
$output .= "Disallow: /wp-login.php
";
$output .= "Disallow: /xmlrpc.php
";
$output .= "Disallow: /wp-includes/
";
$output .= "Disallow: /search/
";
$output .= "Disallow: /?s=
";
$output .= "Allow: /wp-admin/admin-ajax.php
";
$output .= "
";
$output .= "Sitemap: {$sitemap_url}
";
return $output;
}
add_filter( 'robots_txt', 'scseed_custom_robots_txt', 10, 2 );
}#crawling#indexing#robots-txt#security#seo