Uses the wpseo_robots filter from the Yoast SEO plugin to apply strict robots meta directives (noindex, nofollow, noarchive, nosnippet, etc.) to specific page templates such as product and category landing pages. This prevents these pages from appearing in search results or being crawled, making it ideal for handling duplicate, thin, or conversion-focused content. Runs at priority 999 to override other SEO settings.
<?php
add_filter('wpseo_robots', 'yoast_template_noindex', 999);
function yoast_template_noindex($robots = '') {
// Add your page templates here
$templates = [
'your-template-file.php',
'another-template-file.php',
];
// Apply noindex rules if current page uses any listed template
if (is_page_template($templates)) {
return 'noindex, nofollow, noimageindex, noarchive, nosnippet';
}
return $robots;
}
?>