SEO PHP Beginner

Noindex Specific Pages

Add noindex to pages that shouldn't appear in search results, such as thank-you pages and members-only areas.

Tested on WordPress 7.1 PHP 8.3 No plugin needed Updated
functions.php 10 lines
add_filter( 'wp_robots', function ( array $robots ): array {
	// Page slugs to keep out of search results.
	$noindex = [ 'thank-you', 'order-received', 'members' ];

	if ( is_page( $noindex ) ) {
		$robots['noindex'] = true;
		$robots['follow']  = true;
	}
	return $robots;
} );

Where to add this

Recommended A code snippets plugin Survives theme changes and is easy to switch off.
Child theme At the end of your child theme's functions.php file.
Must-use plugin As a .php file in wp-content/mu-plugins, starting with <?php.

Always add PHP to a staging site first, or have FTP or file manager access ready, so you can undo it if something goes wrong.

Thank-you pages, confirmation pages and private areas sometimes end up in Google, where they confuse searchers and inflate conversion tracking. This snippet tells search engines not to index the pages you list.

How it works

WordPress builds its robots meta tag through the wp_robots filter. For pages matching one of the slugs, the snippet adds noindex, follow: search engines drop the page from results but still follow its links.

Things to watch

  • is_page() also accepts IDs and titles, so [ 42, 'thank-you' ] works too.
  • Don’t also block these pages in robots.txt. Search engines must be able to crawl a page to see its noindex tag.
  • SEO plugins have a per-page noindex setting; use that if you have one.

How to test it

View the source of a listed page and look for name='robots'. It should include noindex.

This website uses cookies to enhance your browsing experience and ensure the site functions properly. By continuing to use this site, you acknowledge and accept our use of cookies.

Accept All Accept Required Only