Last active
April 2, 2025 14:22
-
-
Save amboutwe/0c71e42aa164238007d7ea88f174a93f to your computer and use it in GitHub Desktop.
Filters and example code for Yoast SEO robots or WP robots.txt
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
<?php | |
/********* DO NOT COPY THE PARTS ABOVE THIS LINE *********/ | |
/* | |
* Replace Disallow with Allow Generated Robots.txt | |
* Credit: Unknown | |
* Last Tested: June 09 2020 using WordPress 5.4.1 | |
*/ | |
add_filter('robots_txt','custom_robots'); | |
function custom_robots($output) { | |
$public = get_option( 'blog_public' ); | |
if ( '0' != $public ) | |
return str_replace('Disallow','Allow',$output); | |
} |
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
<?php | |
/********* DO NOT COPY THE PARTS ABOVE THIS LINE *********/ | |
/* Check which functions have been used to hook into `wp_robots` | |
* Credit: Yoast team | |
* Last Tested: Unknown | |
*/ | |
\add_filter( 'wp_robots', 'list_hooks' , \PHP_INT_MAX); | |
function list_hooks( $robots ) { | |
global $wp_filter; | |
echo "<!-- This is a list of callback functions hooked into the 'wp_robots' filter:"; | |
echo json_encode($wp_filter['wp_robots'], JSON_PRETTY_PRINT); | |
echo "-->"; | |
return $robots; | |
} |
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
<?php | |
/********* DO NOT COPY THE PARTS ABOVE THIS LINE *********/ | |
/* | |
* Remove robots meta tags from Yoast SEO | |
* Credit: Yoast development team | |
* Last Tested: May 22 2024 using Yoast SEO 22.7 on WordPress 6.5.3 | |
*/ | |
add_filter( 'wpseo_robots', '__return_empty_array' ); // Removes Yoast ouptut and returns to WP core output | |
add_filter( 'wp_robots', '__return_false' ); // Disable core output. Use with above to disable completely. | |
// Older versions | |
add_filter( 'wpseo_robots', '__return_false' ); // Yoast SEO 19.8 or older | |
add_filter( 'wpseo_googlebot', '__return_false' ); // Yoast SEO 14.x - Deprecated | |
add_filter( 'wpseo_bingbot', '__return_false' ); // Yoast SEO 14.x - Deprecated |
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
<?php | |
/********* DO NOT COPY THE PARTS ABOVE THIS LINE *********/ | |
/* | |
* Change meta robots using Yoast SEO | |
* Credit: Yoast development team | |
* Last Tested: Nov 1 2021 using Yoast SEO 17.4 on WordPress 5.8.1 | |
*/ | |
add_filter( 'wpseo_robots', 'yoast_seo_robots_remove_search' ); | |
function yoast_seo_robots_remove_search( $robots ) { | |
if ( is_search() ) { | |
return array(); | |
} else { | |
return $robots; | |
} | |
} |
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
<?php | |
/********* DO NOT COPY THE PARTS ABOVE THIS LINE *********/ | |
/* | |
* Change meta robots using Yoast SEO | |
* Credit: Yoast development team | |
* Last Tested: Dec 12 2017 using Yoast SEO 9.2.1 on WordPress 5.0 | |
********* | |
* DIFFERENT POST TYPES | |
* Post: Change 123456 to the post ID | |
* Page: Change is_single to is_page and 123456 to the page ID | |
* Custom Post Type: Change is_single to is_singular and 123456 to the 'post_type_slug' | |
Example: is_singular( 'cpt_slug' ) | |
********* | |
* MULTIPLE ITEMS | |
* Multiple of the same type can use an array. | |
Example: is_single( array( 123456, 234567, 345678 ) ) | |
* Multiple of different types can repeat the if statement | |
********* | |
* The return empty array removes the robots tag on the page | |
* Or you can return index/noindex follow/nofollow like | |
* return 'noindex, follow'; | |
* Or | |
* return 'noindex, nofollow'; | |
*/ | |
add_filter( 'wpseo_robots', 'yoast_seo_robots_remove_single' ); | |
function yoast_seo_robots_remove_single( $robots ) { | |
if ( is_single ( 123456 ) ) { | |
return array(); | |
} else { | |
return $robots; | |
} | |
} |
@LoganStrike Thanks for bringing this up again. I updated the code to remove the robots
output. The first removes the Yoast changes and the second one removes the WordPress robots
output. Use together to remove the robots
meta tag completely.
Could you test the new code and let me know how it works in a real-world environment, preferably a non-production site?
@amboutwe The updated code works in my tests. May I recommend also updating yoast_seo_robots_remove_search.php and yoast_seo_robots_remove_single.php?
@LoganStrike Done!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Just a warning to anyone using these code snippets to remove the Yoast generated robots tag... before Yoast 19.8, the wpseo_robots filter removed the robots tag when the filter returned false, as shown in a few of the snippets above. But it appears the following PR changed that behaviour to add noindex, nofollow instead of removing the tag. Yoast/wordpress-seo#18488 This change is a major issue for SEO purposes on any site that thought they were removing the robots tag by returning false, as those sites/pages will be noindex,nofollow instead, as of Yoast 19.8. I can;t help but think this was an oversight on Yoast developers. I can't understand why they would change the behaviour of returning false from "don't show the robots tag" to "show a noindex,nofollow robots" tag.