WordPressで自動的に作成されるrobots.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');
        }
    }
}

WordPress Developer Resources

関連機能

  1. サイトマップエラー error on line 2 at column 6: XML declaration allowed only at the start of the document の修正…

  2. WordPress コンソールログの「JQMIGRATE: Migrate is installed, version 3.4.1」を非表示にする

  3. functions.php処理内のデータをdebug.logに出力する

サイドバー

よく使うカスタマイズ

最近の記事

アニメの名言集

「それでも」と言い続けろ
自分を見失うな

機動戦士ガンダムUC
by マリーダ・クルス

Profile

PAGE TOP
Amazon プライム対象