# Add Rules to the Virtual robots.txt

> Adds Disallow rules for internal search result URLs to the robots.txt file that WordPress generates.

- Type: PHP
- Runs: Run everywhere
- Category: [SEO](https://wpide.com/library/category/seo/)
- Tags: seo, robots-txt, crawling
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/robots-txt-rules/

## Code

```php
add_filter( 'robots_txt', function ( $output, $public ) {
    if ( ! $public ) {
        return $output;
    }

    $rules = "Disallow: /?s=\nDisallow: /search/\n";

    if ( false !== strpos( $output, "User-agent: *\n" ) ) {
        return preg_replace( '/User-agent: \*\n/', "User-agent: *\n" . $rules, $output, 1 );
    }

    return "User-agent: *\n" . $rules . "\n" . $output;
}, 10, 2 );
```

## Notes

Only works when there is no physical robots.txt file in the site root.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Add Rules to the Virtual robots.txt" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
