Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use wp_robots() to prevent indexing the error template on WP≥5.7 #414

Merged
merged 1 commit into from
Feb 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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', '>=' ) ) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively we could check for whether the wp_robots function exists to avoid the need for specific version lookup - but I guess the downside of that would be that someone else theoretically could declare a function of that name before. Just a thought, nothing blocking.

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