Last active
July 2, 2025 07:33
-
-
Save FileSubmit/7c51b395047266dd8d1f147c44fddcb0 to your computer and use it in GitHub Desktop.
Noindex to query vars in WooCommerce
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Send an X-Robots-Tag header early. | |
| add_action( 'send_headers', function() { | |
| if ( is_admin() ) { | |
| return; | |
| } | |
| // If this is the WooCommerce “Shop” page, send noindex right away | |
| // If you need the shop page to be indexable, remove | |
| if ( function_exists( 'is_shop' ) && is_shop() ) { | |
| header( 'X-Robots-Tag: noindex, nofollow', true ); | |
| return; | |
| } | |
| // List of exact query‐keys to catch. Add those you need. | |
| $exact_vars = array( | |
| 'vendors', | |
| 'woodmart_reviews_sorting_select', | |
| 'query_type', | |
| 'filter', | |
| 'add-to-cart', | |
| 'product_id', | |
| 'orderby', | |
| 'per_page', | |
| 's', // standard search param | |
| ); | |
| foreach ( $_GET as $key => $value ) { | |
| // If it's exactly in our list, | |
| // OR starts with "filter_", | |
| // OR starts with "query_type_" | |
| if ( | |
| in_array( $key, $exact_vars, true ) | |
| || 0 === strpos( $key, 'filter_' ) | |
| || 0 === strpos( $key, 'query_type_' ) | |
| ) { | |
| header( 'X-Robots-Tag: noindex, nofollow', true ); | |
| break; | |
| } | |
| } | |
| }, 0 ); // very early priority | |
| // This is for RankMath. Adjust the code for Yoast, AIOSEO or other plugins | |
| // Inject the HTML <meta name="robots" content="noindex, nofollow" /> via Rank Math | |
| add_filter( 'rank_math/frontend/robots', function( $robots ) { | |
| if ( is_admin() ) { | |
| return $robots; | |
| } | |
| // If this is the WooCommerce “Shop” page, force noindex/no follow | |
| if ( function_exists( 'is_shop' ) && is_shop() ) { | |
| $robots['index'] = 'noindex'; | |
| $robots['follow'] = 'nofollow'; | |
| return $robots; | |
| } | |
| $exact_vars = array( | |
| 'vendors', | |
| 'woodmart_reviews_sorting_select', | |
| 'query_type', | |
| 'filter', | |
| 'add-to-cart', | |
| 'product_id', | |
| 'orderby', | |
| 'per_page', | |
| 's', // standard search param | |
| ); | |
| foreach ( $_GET as $key => $value ) { | |
| if ( | |
| in_array( $key, $exact_vars, true ) | |
| || 0 === strpos( $key, 'filter_' ) | |
| || 0 === strpos( $key, 'query_type_' ) | |
| ) { | |
| $robots['index'] = 'noindex'; | |
| $robots['follow'] = 'nofollow'; | |
| break; | |
| } | |
| } | |
| return $robots; | |
| } ); | |
| // If you are using Filter Everything Pro, otherwise remove | |
| // Override Filter Everything’s SEO robots meta with an array, not a string | |
| add_filter( 'wpc_seo_robots', 'fe_force_noindex_query_vars', 20 ); | |
| function fe_force_noindex_query_vars( $robots ) { | |
| if ( is_admin() ) { | |
| return $robots; | |
| } | |
| $exact_vars = array( | |
| 'vendors', | |
| 'woodmart_reviews_sorting_select', | |
| 'query_type', | |
| 'filter', | |
| 'add-to-cart', | |
| 'product_id', | |
| 'orderby', | |
| 'per_page', | |
| 's', | |
| ); | |
| foreach ( $_GET as $key => $value ) { | |
| if ( | |
| in_array( $key, $exact_vars, true ) | |
| || 0 === strpos( $key, 'filter_' ) | |
| || 0 === strpos( $key, 'query_type_' ) | |
| ) { | |
| // **return an array** of directives | |
| return array( 'noindex', 'nofollow' ); | |
| } | |
| } | |
| return $robots; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment