Noindex Subsites With a Staging Path
Adds a noindex robots directive to any subsite whose address contains "staging", "dev" or "test", so preview copies inside a network never show up in search results.
Opens WPIDE on
Code
if ( ! is_multisite() ) {
return;
}
$wpide_markers = array( 'staging', 'dev', 'test' ); // Words that mark a preview site.
$wpide_site = get_site();
$wpide_addr = $wpide_site ? strtolower( $wpide_site->domain . $wpide_site->path ) : '';
$wpide_hit = false;
foreach ( $wpide_markers as $wpide_marker ) {
if ( preg_match( '/(^|[.\/-])' . preg_quote( $wpide_marker, '/' ) . '([.\/-]|$)/', $wpide_addr ) ) {
$wpide_hit = true;
break;
}
}
if ( ! $wpide_hit ) {
return;
}
add_filter( 'wp_robots', 'wp_robots_no_robots' );
add_filter( 'robots_txt', function () {
return "User-agent: *\nDisallow: /\n";
}, 99 );
Notes
Edit $wpide_markers to match how you name preview sites.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.