概要説明
カスタマイズの影響またはプラグイン側のなにかの処理で今回の現象が発生した場合のカスタマイズ。
~ 目次 ~
robotx.txtにPタグが自動挿入されてしまう場合の対応
<p>User-agent: * Disallow: /wp-admin/ Allow: /wp-admin/admin-ajax.php </p>
https://サイトURL/robots.txt
WordPressでは上記URLにて自動的にrobots.txtコンテンツを作成しています
特定のURLの時だけを判定する
if(strstr($_SERVER['REQUEST_URI'], 'robots.txt')){
// URLにrobots.txt が含まれるはこちらの判定条件になります
}
コード – robots.txtの出力前にフックして調整する
functions.php
/*----------------------------------------------------
robots.txt 表示前に内容を調整する
----------------------------------------------------*/
if ( !function_exists( 'robots_txt_replace' ) ){
if (isset($_SERVER['REQUEST_URI'])) {
if(strstr($_SERVER['REQUEST_URI'], 'robots.txt')){
function robots_txt_replace($buffer) {
$buffer = str_replace('<p>', '', $buffer);
$buffer = str_replace('</p>', '', $buffer);
return $buffer;
}
function buf_start() { ob_start('robots_txt_replace'); }
function buf_end() {
if( ob_get_length() ){
ob_end_flush();
}
}
add_action('after_setup_theme', 'buf_start');
add_action('shutdown', 'buf_end');
}
}
}



