From 149c00006d12dd95211ece2b50f98148ee8fbebe Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Mon, 1 Feb 2021 13:37:43 -0800 Subject: [PATCH] =?UTF-8?q?Use=20wp=5Frobots()=20to=20prevent=20indexing?= =?UTF-8?q?=20the=20error=20template=20on=20WP=E2=89=A55.7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- wp-includes/default-filters.php | 9 +++++++-- wp-includes/general-template.php | 16 ++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/wp-includes/default-filters.php b/wp-includes/default-filters.php index 9124f1e87..ee7328c44 100644 --- a/wp-includes/default-filters.php +++ b/wp-includes/default-filters.php @@ -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' ); diff --git a/wp-includes/general-template.php b/wp-includes/general-template.php index 93c3293a3..32c16ec63 100644 --- a/wp-includes/general-template.php +++ b/wp-includes/general-template.php @@ -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