do_robots() - robots.txtの内容を出力する
公開日:
do_robots()
は、robots.txtの内容を出力する関数です。そのままecho
で出力するため、返り値はありません。
構文
do_robots()
パラメータ
引数はありません。
返り値
返り値はありません。
関数
この関数内で利用している、別のWordPress関数です。
- do_action()
- echoapply_filters()
- get_option()
フック
関数内で呼び出されるイベントです。
フィルター
robots_txt
サンプルコード
robots.txtの内容を出力する
robots.txtの内容を出力します。内部でecho
が実行されているため、返り値はありません。
php
<?php
// 実行
do_robots() ;
結果 (出力内容) - PHP7.0.13
User-agent: *
Disallow: /
ソースコード
wp-includes/functions.php
/**
* Display the robots.txt file content.
*
* The echo content should be with usage of the permalinks or for creating the
* robots.txt file.
*
* @since 2.1.0
*/
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' );
if ( '0' == $public ) {
$output .= "Disallow: /\n";
} else {
$site_url = parse_url( site_url() );
$path = ( !empty( $site_url['path'] ) ) ? $site_url['path'] : '';
$output .= "Disallow: $path/wp-admin/\n";
$output .= "Allow: $path/wp-admin/admin-ajax.php\n";
}
/**
* Filters the robots.txt output.
*
* @since 3.0.0
*
* @param string $output Robots.txt output.
* @param bool $public Whether the site is considered "public".
*/
echo apply_filters( 'robots_txt', $output, $public );
}
参考リンク
- WordPress 関数リファレンス - 公式マニュアル。