Noindex Password-Protected Posts and Pages
Adds noindex to posts and pages that need a password, so search engines do not list pages whose content they cannot see.
Opens WPIDE on
Code
add_filter( 'wp_robots', function ( $robots ) {
if ( is_singular() && '' !== (string) get_post_field( 'post_password', get_queried_object_id() ) ) {
$robots['noindex'] = true;
$robots['follow'] = true;
}
return $robots;
} );
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.