Skip to content

Commit ee498d6

Browse files
committed
Robots: Add noindex to 404 pages
1 parent 67094ee commit ee498d6

4 files changed

Lines changed: 53 additions & 0 deletions

File tree

src/wp-includes/default-filters.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@
277277
add_filter( 'wp_robots', 'wp_robots_noindex' );
278278
add_filter( 'wp_robots', 'wp_robots_noindex_embeds' );
279279
add_filter( 'wp_robots', 'wp_robots_noindex_search' );
280+
add_filter( 'wp_robots', 'wp_robots_noindex_404' );
280281
add_filter( 'wp_robots', 'wp_robots_max_image_preview_large' );
281282

282283
// Mark site as no longer fresh.

src/wp-includes/functions.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3930,6 +3930,7 @@ function _default_wp_die_handler( $message, $title = '', $args = array() ) {
39303930
// Prevent warnings because of $wp_query not existing.
39313931
remove_filter( 'wp_robots', 'wp_robots_noindex_embeds' );
39323932
remove_filter( 'wp_robots', 'wp_robots_noindex_search' );
3933+
remove_filter( 'wp_robots', 'wp_robots_noindex_404' );
39333934
wp_robots();
39343935
}
39353936
?>

src/wp-includes/robots-template.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,32 @@ function wp_robots_noindex_search( array $robots ) {
123123
return $robots;
124124
}
125125

126+
/**
127+
* Adds `noindex` to the robots meta tag if a 404 error has occurred.
128+
*
129+
* If a 404 error has occurred then noindex will be output to
130+
* tell web robots not to index the page content. Add this to the
131+
* {@see 'wp_robots'} filter.
132+
*
133+
* Typical usage is as a {@see 'wp_robots'} callback:
134+
*
135+
* add_filter( 'wp_robots', 'wp_robots_noindex_404' );
136+
*
137+
* @since x.x.x
138+
*
139+
* @see wp_robots_no_robots()
140+
*
141+
* @param array $robots Associative array of robots directives.
142+
* @return array Filtered robots directives.
143+
*/
144+
function wp_robots_noindex_404( array $robots ) {
145+
if ( is_404() ) {
146+
return wp_robots_no_robots( $robots );
147+
}
148+
149+
return $robots;
150+
}
151+
126152
/**
127153
* Adds `noindex` to the robots meta tag.
128154
*

tests/phpunit/tests/robots.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class Tests_Robots extends WP_UnitTestCase {
1111
public function set_up() {
1212
parent::set_up();
1313

14+
$this->set_permalink_structure( '/%postname%/' );
1415
remove_all_filters( 'wp_robots' );
1516
}
1617

@@ -144,6 +145,30 @@ public function test_wp_robots_non_search_page() {
144145
$this->assertStringNotContainsString( 'noindex', $output );
145146
}
146147

148+
/**
149+
* @ticket 58751
150+
* @covers ::wp_robots_noindex_404
151+
*/
152+
public function test_wp_robots_404_page() {
153+
add_filter( 'wp_robots', 'wp_robots_noindex_404' );
154+
$this->go_to( '/this-page-does-not-exist' );
155+
156+
$output = get_echo( 'wp_robots' );
157+
$this->assertStringContainsString( 'noindex', $output );
158+
}
159+
160+
/**
161+
* @ticket 58751
162+
* @covers ::wp_robots_noindex_404
163+
*/
164+
public function test_wp_robots_non_404_page() {
165+
add_filter( 'wp_robots', 'wp_robots_noindex_404' );
166+
$this->go_to( home_url() );
167+
168+
$output = get_echo( 'wp_robots' );
169+
$this->assertStringNotContainsString( 'noindex', $output );
170+
}
171+
147172
public function add_noindex_directive( array $robots ) {
148173
$robots['noindex'] = true;
149174
return $robots;

0 commit comments

Comments
 (0)