钩子文档

robots_txt

💡 云策文档标注

概述

本文档介绍了 WordPress 中的 'robots_txt' 过滤器,用于修改 robots.txt 文件的输出内容。该过滤器允许开发者根据网站是否公开等条件,动态调整搜索引擎爬虫的访问规则。

关键要点

  • 'robots_txt' 过滤器用于过滤 robots.txt 的输出,参数包括 $output(输出字符串)和 $public(网站是否公开的布尔值)。
  • 过滤器通过 apply_filters('robots_txt', $output, $public) 调用,在 do_robots() 函数中显示默认内容。
  • 自 WordPress 3.0.0 版本引入,开发者可以通过添加过滤器函数来定制 robots.txt 内容,例如添加 Disallow 规则或 Sitemap 链接。

代码示例

add_filter( 'robots_txt', function( $output, $public ) {
    if ( '0' == $public ) {
        $output = "User-agent: *nDisallow: /nDisallow: /*nDisallow: /*?n";
    } else {
        $site_url = parse_url( site_url() );
        $path = ( ! empty( $site_url['path'] ) ) ? $site_url['path'] : '';
        $output .= "Disallow: $path/wp-login.phpn";
        foreach(['jpeg','jpg','gif','png','mp4','webm','woff','woff2','ttf','eot'] as $ext) {
            $output .= "Disallow: /*.{$ext}$n";
        }
        $robots = preg_replace( '/Allow: [^s]*/wp-admin/admin-ajax.phpn/', '', $output );
        if ( null !== $robots ) {
            $output = $robots;
        }
        $output .= "Sitemap: {$site_url['scheme']}://{$site_url['host']}/sitemap_index.xmln";
    }
    return $output;
}, 99, 2 );

注意事项

  • 使用过滤器时需注意优先级(如示例中的 99)和参数数量(如示例中的 2),以确保正确执行。
  • 在修改 robots.txt 内容时,应遵循 robots.txt 协议规范,避免影响搜索引擎的正常爬取。
  • 示例代码展示了如何根据 $public 参数处理公开和非公开网站的不同情况,开发者可根据实际需求调整规则。

📄 原文内容

Filters the robots.txt output.

Parameters

$outputstring
The robots.txt output.
$publicbool
Whether the site is considered “public”.

Source

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

Changelog

Version Description
3.0.0 Introduced.

User Contributed Notes

  1. Skip to note 2 content

    An example of how robots.txt file content can be altered.

    /**
     * Add Disallow for some file types.
     * Add "Disallow: /wp-login.php/n".
     * Remove "Allow: /wp-admin/admin-ajax.phpn".
     * 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.phpn";
    
    		/**
    		 * 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: [^s]*/wp-admin/admin-ajax.phpn/', '', $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.xmln";
    	}
    
    	return $output;
    
    }, 99, 2 );  // Priority 99, Number of Arguments 2.