Proper implementation of wp_robots_no_robots

This commit is contained in:
Claudio Sanches 2021-02-01 19:15:10 -03:00
parent f9eb0727d5
commit 31af5530e0
1 changed files with 24 additions and 6 deletions

View File

@ -3564,20 +3564,38 @@ function wc_empty_cart_message() {
/**
* Disable search engines indexing core, dynamic, cart/checkout pages.
*
* @todo Deprecated this function after dropping support for WP 5.6.
* @since 3.2.0
*/
function wc_page_noindex() {
// wp_no_robots is deprecated since WP 5.7.
if ( ! function_exists( 'wp_robots_no_robots' ) ) {
return;
}
if ( is_page( wc_get_page_id( 'cart' ) ) || is_page( wc_get_page_id( 'checkout' ) ) || is_page( wc_get_page_id( 'myaccount' ) ) ) {
// Adds support for WP 5.7.
if ( function_exists( 'wp_robots_no_robots' ) ) {
wp_robots_no_robots();
} else {
wp_no_robots();
}
wp_no_robots();
}
}
add_action( 'wp_head', 'wc_page_noindex' );
/**
* Disable search engines indexing core, dynamic, cart/checkout pages.
* Uses "wp_robots" filter introduced in WP 5.7.
*
* @since 5.0.0
* @param array $robots Associative array of robots directives.
* @return array Filtered robots directives.
*/
function wc_page_no_robots( $robots ) {
if ( is_page( wc_get_page_id( 'cart' ) ) || is_page( wc_get_page_id( 'checkout' ) ) || is_page( wc_get_page_id( 'myaccount' ) ) ) {
return wp_robots_no_robots( $robots );
}
return $robots;
}
add_filter( 'wp_robots', 'wc_page_no_robots' );
/**
* Get a slug identifying the current theme.
*