Skip to content

Instantly share code, notes, and snippets.

@amboutwe
Last active March 20, 2024 15:22
Show Gist options
  • Star 35 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save amboutwe/0c71e42aa164238007d7ea88f174a93f to your computer and use it in GitHub Desktop.
Save amboutwe/0c71e42aa164238007d7ea88f174a93f to your computer and use it in GitHub Desktop.
Filters and example code for Yoast SEO robots or WP robots.txt
<?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);
}
<?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;
}
/*
* Change meta robots to index all author archive pages
* Credit: Yoast development team
* Last Tested: Jul 20 2023 using Yoast SEO 20.11 on WordPress 6.2.2
*/
add_filter( 'wpseo_robots', 'yoast_seo_robots_change_author_archive' );
function yoast_seo_robots_change_author_archive( $robots ) {
if ( is_author() ) {
return 'index, follow, max-image-preview:large, max-snippet:-1, max-video-preview:-1';
} else {
return $robots;
}
}
<?php
/********* DO NOT COPY THE PARTS ABOVE THIS LINE *********/
/*
* Remove robots meta tags from Yoast SEO
* Credit: Yoast development team
* Last Tested: May 09 2020 using Yoast SEO 14.0 on WordPress 5.4.1
*/
add_filter( 'wpseo_robots', '__return_false' );
add_filter( 'wpseo_googlebot', '__return_false' ); // Yoast SEO 14.x or newer
add_filter( 'wpseo_bingbot', '__return_false' ); // Yoast SEO 14.x or newer
<?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 false;
} else {
return $robots;
}
}
<?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 false 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 false;
} else {
return $robots;
}
}
@HesterSchoeman
Copy link

HesterSchoeman commented Feb 8, 2024

Hi there, I need some help. We have Yoast installed on our website and I need to add a noarchive to the WP Fusion restricted posts. The condition is definately working . I tested the condition with an echo statement and it does add the ", noarchive" to the $robots string, but it does not update the robot. We can add the noarchive manually to the posts, but we need to automate it to ensure that it is present when there is a restriction on the post. What would the reason be for it not updating the robot?

add_filter('wpseo_robots', 'add_noarchive_directive');
function add_noarchive_directive($robots) {
 	global $post;
	$post_id = $post->ID;
    // Check if the current post is restricted for the user
     if (wp_fusion()->access->get_post_access_meta( $post_id )['lock_content'] ) {
			// Add 'noarchive' directive if the condition is met
        	$robots .= ', noarchive';
    }
    return $robots;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment