View Single Post
Old 04-13-2025, 07:52 PM  
mightyjoe
Confirmed User
 
Join Date: Jun 2004
Posts: 1,395
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');
mightyjoe is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote