do_robots()

最后更新于:2021-11-26 03:58:10

do_robots()

Displays the default robots.txt file content.

源文件

文件: gc-includes/functions.php

function do_robots() {
	header( 'Content-Type: text/plain; charset=utf-8' );

	/**
	 * Fires when displaying the robots.txt file.
	 *
	 * @since 2.1.0
	 */
	do_action( 'do_robotstxt' );

	$output = "User-agent: *n";
	$public = get_option( 'blog_public' );

	$site_url = parse_url( site_url() );
	$path     = ( ! empty( $site_url['path'] ) ) ? $site_url['path'] : '';
	$output  .= "Disallow: $path/gc-admin/n";
	$output  .= "Allow: $path/gc-admin/admin-ajax.phpn";

	/**
	 * Filters the robots.txt output.
	 *
	 * @since 3.0.0
	 *
	 * @param string $output The robots.txt output.
	 * @param bool   $public Whether the site is considered "public".
	 */
	echo apply_filters( 'robots_txt', $output, $public );
}
/**
 * Add Disallow for some file types.
 * Add "Disallow: /gc-login.php/n".
 * Remove "Allow: /gc-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 {
		/**
		 * Disallow some file types
		 */
		foreach( array( 'jpeg','jpg','gif','png','mp4','webm','woff','woff2','ttf','eot' ) as $ext ) {
			$output .= "Disallow: /*.{$ext}$n";
		}

		/**
		 * Get site path.
		 */
		$site_url = parse_url( site_url() );
		$path	  = ( ! empty( $site_url['path'] ) ) ? $site_url['path'] : '';

		/**
		 * Add new disallow.
		 */
		$output .= "Disallow: $path/gc-login.phpn";

		/**
		 * Remove line that allows robots to access AJAX interface.
		 */
		$robots = preg_replace( '/Allow: [^s]*/gc-admin/admin-ajax.phpn/', '', $output );

		/**
		 * If no error occurred, replace $output with modified value.
		 */
		if ( ! is_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.