Skip to content

Commit

Permalink
#43590: Use robots meta tag to better discourage search engines.
Browse files Browse the repository at this point in the history
This changes the "discourage search engines" option to output a `noindex, nofollow` robots meta tag. `Disallow: /` is removed from the `robots.txt` to allow search engines to discover they are requested not to index the site.

Disallowing search engines from accessing a site in the `robots.txt` file can result in search engines listing a site with a fragment (a listing without content).

Props donmhico, jonoaldersonwp.
Fixes #43590.



git-svn-id: https://develop.svn.wordpress.org/trunk@45928 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
peterwilsoncc committed Sep 2, 2019
1 parent d0db5be commit 122cb28
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
15 changes: 7 additions & 8 deletions src/wp-includes/functions.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -1582,6 +1582,8 @@ function do_feed_atom( $for_comments ) {
* Displays the default robots.txt file content. * Displays the default robots.txt file content.
* *
* @since 2.1.0 * @since 2.1.0
* @since 5.3.0 Remove the "Disallow: /" output if search engine visiblity is
* discouraged in favor of robots meta HTML tag in wp_no_robots().
*/ */
function do_robots() { function do_robots() {
header( 'Content-Type: text/plain; charset=utf-8' ); header( 'Content-Type: text/plain; charset=utf-8' );
Expand All @@ -1595,14 +1597,11 @@ function do_robots() {


$output = "User-agent: *\n"; $output = "User-agent: *\n";
$public = get_option( 'blog_public' ); $public = get_option( 'blog_public' );
if ( '0' == $public ) {
$output .= "Disallow: /\n"; $site_url = parse_url( site_url() );
} else { $path = ( ! empty( $site_url['path'] ) ) ? $site_url['path'] : '';
$site_url = parse_url( site_url() ); $output .= "Disallow: $path/wp-admin/\n";
$path = ( ! empty( $site_url['path'] ) ) ? $site_url['path'] : ''; $output .= "Allow: $path/wp-admin/admin-ajax.php\n";
$output .= "Disallow: $path/wp-admin/\n";
$output .= "Allow: $path/wp-admin/admin-ajax.php\n";
}


/** /**
* Filters the robots.txt output. * Filters the robots.txt output.
Expand Down
10 changes: 8 additions & 2 deletions src/wp-includes/general-template.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2986,12 +2986,18 @@ function noindex() {
* Display a noindex meta tag. * Display a noindex meta tag.
* *
* Outputs a noindex meta tag that tells web robots not to index the page content. * Outputs a noindex meta tag that tells web robots not to index the page content.
* Typical usage is as a wp_head callback. add_action( 'wp_head', 'wp_no_robots' ); * Typical usage is as a {@see 'wp_head'} callback. add_action( 'wp_head', 'wp_no_robots' );
* *
* @since 3.3.0 * @since 3.3.0
* @since 5.3.0 Echo "noindex,nofollow" if search engine visibility is discouraged.
*/ */
function wp_no_robots() { function wp_no_robots() {
echo "<meta name='robots' content='noindex,follow' />\n"; if ( get_option( 'blog_public' ) ) {
echo "<meta name='robots' content='noindex,follow' />\n";
return;
}

echo "<meta name='robots' content='noindex,nofollow' />\n";
} }


/** /**
Expand Down
15 changes: 15 additions & 0 deletions tests/phpunit/tests/general/template.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -612,4 +612,19 @@ function test_get_custom_logo_preserves_switched_state() {


$this->assertSame( $expected, $result ); $this->assertSame( $expected, $result );
} }

/**
* @ticket 43590
*/
function test_wp_no_robots() {
// Simulate private site (search engines discouraged).
update_option( 'blog_public', '0' );
$actual_private = get_echo( 'wp_no_robots' );
$this->assertSame( "<meta name='robots' content='noindex,nofollow' />\n", $actual_private );

// Simulate public site.
update_option( 'blog_public', '1' );
$actual_public = get_echo( 'wp_no_robots' );
$this->assertSame( "<meta name='robots' content='noindex,follow' />\n", $actual_public );
}
} }

0 comments on commit 122cb28

Please sign in to comment.