Skip to content

Instantly share code, notes, and snippets.

@robwent
Created April 25, 2018 12:02
Show Gist options
  • Save robwent/f6d95ddd19d38a936c0d0ad5372081d8 to your computer and use it in GitHub Desktop.
Save robwent/f6d95ddd19d38a936c0d0ad5372081d8 to your computer and use it in GitHub Desktop.
Removes WooCommerce noindex robots tag on public pages
function YOURPREFIX_remove_wc_account_page_noindex(){
if (!is_user_logged_in()) {
remove_action( 'wp_head', 'wc_page_noindex' );
}
}
add_action( 'init', 'YOURPREFIX_remove_wc_account_page_noindex' );
@srjlulla
Copy link

srjlulla commented Mar 30, 2022

Since WP 5.7, Woocommerce uses the wp_robots filter. If remove_action( 'wp_head', 'wc_page_noindex' ) did not work for you, then you can try the following:

    // Remove WooCommerce noindex meta in cart, checkout and myaccount pages
    add_action( 'template_redirect', 'srj_woo_remove_noindex' );
    function srj_woo_remove_noindex() {
      if ( is_page( wc_get_page_id( 'cart' ) ) || is_page( wc_get_page_id( 'checkout' ) ) || is_page( wc_get_page_id( 'myaccount' ) ) ) {
        remove_filter( 'wp_robots', 'wc_page_no_robots', 10 );
      }
    }

@robwent
Copy link
Author

robwent commented Mar 30, 2022

@srjlulla Thanks for the update.

I would probably leave out the cart and checkout since those are dynamic pages. If search engines did find them (sitemap?) then they would be indexing an empty cart page and a checkout with no products.

@iamalexm
Copy link

Since WP 5.7, Woocommerce uses the wp_robots filter. If remove_action( 'wp_head', 'wc_page_noindex' ) did not work for you, then you can try the following:

    // Remove WooCommerce noindex meta in cart, checkout and myaccount pages
    add_action( 'template_redirect', 'srj_woo_remove_noindex' );
    function srj_woo_remove_noindex() {
      if ( is_page( wc_get_page_id( 'cart' ) ) || is_page( wc_get_page_id( 'checkout' ) ) || is_page( wc_get_page_id( 'myaccount' ) ) ) {
        remove_filter( 'wp_robots', 'wc_page_no_robots', 10 );
      }
    }

@srjlulla thanks for the snippet but it doesn't work for me. Do you know if there's another way to remove the noindex meta?

@srjlulla
Copy link

@iamalexm are you sure any other code/function/plugin isn't overriding it?

@iamalexm
Copy link

@srjlulla yes pretty sure. I used a troobleshooting plugin, all other plugins are deactivated exept rank math, rank math pro and woocommerce.

With woocommerce deactivated my meta looks like this:
<meta name="robots" content="follow, index, max-snippet:-1, max-video-preview:-1, max-image-preview:large">

With woocommerce activated :
<meta name="robots" content="follow, noindex">

I tried both snippet above but none are working.

@iamalexm
Copy link

iamalexm commented Aug 16, 2022

@srjlulla after further research it was the rank math plugin who was considering my pages as checkout because of some code. So if you're using rank math and have this noindex issue you can use the next snippet :

add_filter( 'rank_math/frontend/robots', function( $robots ) { if( is_checkout() ) { $robots['index'] = 'index'; $robots['follow'] = 'follow'; } return $robots; });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment