and this is the code to noindex all the pages with ? except ?s=
chatgpt is great actually if you have clear prompt
function replace_robots_meta_tag_for_query_pages_except_sea rch($buffer) {
// Skip search pages (?s=)
if ( isset($_GET['s']) && !empty($_GET['s']) ) {
return $buffer;
}
// If the URL contains a query string
if ( strpos($_SERVER['REQUEST_URI'], '?') !== false ) {
// Replace any existing robots meta tag
$buffer = preg_replace(
'/<meta\s+name=[\'"]robots[\'"]\s+content=[\'"][^\'"]+[\'"]\s*\/?>/i',
"<meta name='robots' content='noindex, follow' />",
$buffer
);
}
return $buffer;
}
function start_replace_robots_buffer_for_query_pages_except _search() {
ob_start('replace_robots_meta_tag_for_query_pages_ except_search');
}
add_action('template_redirect', 'start_replace_robots_buffer_for_query_pages_excep t_search');
|