Skip to content

Commit

Permalink
Merge pull request #414 from GoogleChromeLabs/fix/wp-robots-5.7
Browse files Browse the repository at this point in the history
Use wp_robots() to prevent indexing the error template on WP≥5.7
  • Loading branch information
westonruter authored Feb 1, 2021
2 parents 95ecf36 + 149c000 commit e748079
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
9 changes: 7 additions & 2 deletions wp-includes/default-filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@
add_action( 'wp_ajax_nopriv_wp_service_worker', 'wp_ajax_wp_service_worker' );
add_action( 'parse_query', 'wp_unauthenticate_error_template_requests' );

add_action( 'wp_head', 'wp_add_error_template_no_robots' );
add_action( 'error_head', 'wp_add_error_template_no_robots' );
if ( version_compare( strtok( get_bloginfo( 'version' ), '-' ), '5.7', '>=' ) ) {
add_action( 'error_head', 'wp_robots', 1 ); // To match wp_robots running at wp_head.
add_filter( 'wp_robots', 'wp_filter_robots_for_error_template' );
} else {
add_action( 'wp_head', 'wp_add_error_template_no_robots' );
add_action( 'error_head', 'wp_add_error_template_no_robots' );
}

add_action( 'admin_init', 'wp_disable_script_concatenation' );
16 changes: 16 additions & 0 deletions wp-includes/general-template.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,25 @@ function pwa_get_footer( $name = null ) {
// End core patch.
}

/**
* Filter wp_robots to prevent the error template from being indexed.
*
* @since 0.7
*
* @param array $robots Robots.
* @return array Robots.
*/
function wp_filter_robots_for_error_template( $robots ) {
if ( is_offline() || is_500() ) {
$robots['noindex'] = true;
}
return $robots;
}

/**
* Add no-robots meta tag to error template.
*
* @deprecated Only relevant to WordPress < 5.6.
* @todo Is this right? Should we add_action when we find out that the filter is present?
* @see wp_no_robots()
* @since 0.2
Expand Down

0 comments on commit e748079

Please sign in to comment.