Skip to content

Commit 07bfe37

Browse files
committed
Robots: Use admin_url() for the robots.txt admin paths.
Derive the `Disallow` and `Allow` directives in `do_robots()` from `admin_url()` and `admin_url( 'admin-ajax.php' )` rather than from a hardcoded `/wp-admin/` path built off `site_url()`. Only the URL path portion is emitted, as before, so installs that relocate or filter their admin URL now produce a correct default `robots.txt`. Also guard the `Content-Type` header with a `headers_sent()` check to avoid a warning when the headers have already been sent. Developed in WordPress#11998. Follow-up to r34985. Props masteradhoc, yogeshbhutkar, hrohh, westonruter, mukeshpanchal27, 1ucay. Fixes #63467. git-svn-id: https://develop.svn.wordpress.org/trunk@62633 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 97e8e28 commit 07bfe37

2 files changed

Lines changed: 25 additions & 5 deletions

File tree

src/wp-includes/functions.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1711,7 +1711,9 @@ function do_feed_atom( $for_comments ) {
17111711
* filter callback.
17121712
*/
17131713
function do_robots() {
1714-
header( 'Content-Type: text/plain; charset=utf-8' );
1714+
if ( ! headers_sent() ) {
1715+
header( 'Content-Type: text/plain; charset=utf-8' );
1716+
}
17151717

17161718
/**
17171719
* Fires when displaying the robots.txt file.
@@ -1723,10 +1725,8 @@ function do_robots() {
17231725
$output = "User-agent: *\n";
17241726
$public = (bool) get_option( 'blog_public' );
17251727

1726-
$site_url = parse_url( site_url() );
1727-
$path = ( ! empty( $site_url['path'] ) ) ? $site_url['path'] : '';
1728-
$output .= "Disallow: $path/wp-admin/\n";
1729-
$output .= "Allow: $path/wp-admin/admin-ajax.php\n";
1728+
$output .= 'Disallow: ' . wp_parse_url( admin_url(), PHP_URL_PATH ) . "\n";
1729+
$output .= 'Allow: ' . wp_parse_url( admin_url( 'admin-ajax.php' ), PHP_URL_PATH ) . "\n";
17301730

17311731
/**
17321732
* Filters the robots.txt output.

tests/phpunit/tests/robots.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,26 @@ public function test_wp_robots_non_search_page() {
144144
$this->assertStringNotContainsString( 'noindex', $output );
145145
}
146146

147+
/**
148+
* @ticket 63467
149+
*/
150+
public function test_do_robots_uses_filtered_admin_url_paths(): void {
151+
add_filter(
152+
'admin_url',
153+
static function ( string $url, string $path, ?int $blog_id, string $scheme ): string {
154+
return home_url( "/control/$path", $scheme );
155+
},
156+
10,
157+
4
158+
);
159+
160+
$output = get_echo( 'do_robots' );
161+
162+
$this->assertStringNotContainsString( 'wp-admin', $output );
163+
$this->assertStringContainsString( "Disallow: /control/\n", $output );
164+
$this->assertStringContainsString( "Allow: /control/admin-ajax.php\n", $output );
165+
}
166+
147167
public function add_noindex_directive( array $robots ) {
148168
$robots['noindex'] = true;
149169
return $robots;

0 commit comments

Comments
 (0)