Title: robots_txt
Published: April 25, 2014
Last modified: February 24, 2026

---

# apply_filters( ‘robots_txt’, string $output, bool $public )

## In this article

 * [Parameters](https://developer.wordpress.org/reference/hooks/robots_txt/?output_format=md#parameters)
 * [Source](https://developer.wordpress.org/reference/hooks/robots_txt/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/hooks/robots_txt/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/hooks/robots_txt/?output_format=md#changelog)
 * [User Contributed Notes](https://developer.wordpress.org/reference/hooks/robots_txt/?output_format=md#user-contributed-notes)

[ Back to top](https://developer.wordpress.org/reference/hooks/robots_txt/?output_format=md#wp--skip-link--target)

Filters the robots.txt output.

## 󠀁[Parameters](https://developer.wordpress.org/reference/hooks/robots_txt/?output_format=md#parameters)󠁿

 `$output`string

The robots.txt output.

`$public`bool

Whether the site is considered "public".

## 󠀁[Source](https://developer.wordpress.org/reference/hooks/robots_txt/?output_format=md#source)󠁿

    ```php
    echo apply_filters( 'robots_txt', $output, $public );
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/functions.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-includes/functions.php#L1734)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/functions.php#L1734-L1734)

## 󠀁[Related](https://developer.wordpress.org/reference/hooks/robots_txt/?output_format=md#related)󠁿

| Used by | Description | 
| [do_robots()](https://developer.wordpress.org/reference/functions/do_robots/)`wp-includes/functions.php` |

Displays the default robots.txt file content.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/hooks/robots_txt/?output_format=md#changelog)󠁿

| Version | Description | 
| [3.0.0](https://developer.wordpress.org/reference/since/3.0.0/) | Introduced. |

## 󠀁[User Contributed Notes](https://developer.wordpress.org/reference/hooks/robots_txt/?output_format=md#user-contributed-notes)󠁿

 1.  [Skip to note 2 content](https://developer.wordpress.org/reference/hooks/robots_txt/?output_format=md#comment-content-4689)
 2.   [wp_kc](https://profiles.wordpress.org/wp_kc/)  [  5 years ago  ](https://developer.wordpress.org/reference/hooks/robots_txt/#comment-4689)
 3. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Frobots_txt%2F%23comment-4689)
    Vote results for this note: 3[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Frobots_txt%2F%23comment-4689)
 4. An example of how robots.txt file content can be altered.
 5.     ```php
        /**
         * Add Disallow for some file types.
         * Add "Disallow: /wp-login.php/\n".
         * Remove "Allow: /wp-admin/admin-ajax.php\n".
         * Calculate and add a "Sitemap:" link.
         */
        add_filter( 'robots_txt', function( $output, $public ) {
        	/**
        	 * If "Search engine visibility" is disabled,
        	 * strongly tell all robots to go away.
        	 */
        	if ( '0' == $public ) {
    
        		$output = "User-agent: *\nDisallow: /\nDisallow: /*\nDisallow: /*?\n";
    
        	} else {
    
        		/**
        		 * Get site path.
        		 */
        		$site_url = parse_url( site_url() );
        		$path	  = ( ! empty( $site_url[ 'path' ] ) ) ? $site_url[ 'path' ] : '';
    
        		/**
        		 * Add new disallow.
        		 */
        		$output .= "Disallow: $path/wp-login.php\n";
    
        		/**
        		 * Disallow some file types
        		 */
        		foreach(['jpeg','jpg','gif','png','mp4','webm','woff','woff2','ttf','eot'] as $ext){
        			$output .= "Disallow: /*.{$ext}$\n";
        		}
    
        		/**
        		 * Remove line that allows robots to access AJAX interface.
        		 */
        		$robots = preg_replace( '/Allow: [^\0\s]*\/wp-admin\/admin-ajax\.php\n/', '', $output );
    
        		/**
        		 * If no error occurred, replace $output with modified value.
        		 */
        		if ( null !== $robots ) {
        			$output = $robots;
        		}
        		/**
        		 * Calculate and add a "Sitemap:" link.
        		 * Modify as needed.
        		 */
        		$output .= "Sitemap: {$site_url[ 'scheme' ]}://{$site_url[ 'host' ]}/sitemap_index.xml\n";
        	}
    
        	return $output;
    
        }, 99, 2 );  // Priority 99, Number of Arguments 2.
        ```
    
 6.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Frobots_txt%2F%3Freplytocom%3D4689%23feedback-editor-4689)

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Frobots_txt%2F)
before being able to contribute a note or feedback.