Featured image of post 【原因はStaticPress】吐き出したAMPページがGoogleSearchConsoleで「<meta http-equiv>」が原因でエラー発生する際の対応方法【Wordpress】

【原因はStaticPress】吐き出したAMPページがGoogleSearchConsoleで「<meta http-equiv>」が原因でエラー発生する際の対応方法【Wordpress】

1276文字

SearchConsolsでAMPエラー

StaticPressが出力したAMPページのHTML構造がダメらしい

先日、Wordpressの「AMP for WordPress」プラグインを利用して、サイトをAMP対応する方法についてご紹介しました。

関連記事 【StaticPressも対応】Wordpressのサイトを「AMP for WordPress」プラグインを使ってサクッとAMP対応しちゃおう【インストール/使い方】 2018/11/15 2201文字 約5分で読めます

AMPページもしっかり転送されていましたが、HTMLの内容に不備があるらしく、GoogleSearchConsolsにてエラーが出るようになりました。

全部エラー。。。orz

原因はStaticPressの更新日時追加処理

エラーの内容を見てみると、以下のようになっています。

なにやら、<meta http-equiv="Last-Modified" content="...">のタグにAMPでは使えない属性であるhttp-equivを含んでしまっているようですね。

このタグは、Wordpressで表示している際のHTMLには存在していません。

これは、StaticPressがHTMLファイルを生成する際に自動で追加されるタグなのです!!

wp-content/plugins/staticpress/includes/class-static_press.php
// 461行目付近
public function	add_last_modified($content, $http_code = 200) {
		if (intval($http_code) === 200) {
			$type = preg_match('#<!DOCTYPE html>#i', $content) ? 'html' : 'xhtml';
			switch ( $type ) {
			case 'html':
				$last_modified = sprintf('<meta http-equiv="Last-Modified" content="%s GMT">', gmdate("D, d M Y H:i:s"));
				break;
			case 'xhtml':
			default:
				$last_modified = sprintf('<meta http-equiv="Last-Modified" content="%s GMT" />', gmdate("D, d M Y H:i:s"));
				break;
			}
                        // ここで追加されている
			$content = preg_replace('#(<head>|<head [^>]+>)#ism', '$1'."\n".$last_modified, $content);
		}
		return $content;
	}

なるほど。。。

原因が分かりましたので、AMPページの場合はこの処理を行わないように修正してみましょう。

手順

class-static_press.phpの修正

まずは、AMPページの場合にエラーの原因のタグを追加しないようにclass-static_press.php373行目辺りと465行目あたりに処理を修正します。

wp-content/plugins/staticpress/includes/class-static_press.php
// 373行目辺り
switch ($file_type) {
		case 'front_page':
		case 'single':
		case 'term_archive':
		case 'author_archive':
                case 'seo_files':
		case 'other_page':
			// AMP判定
			$haystack = $url; // 追加
			$needle = "amp/";// 追加
			$amp_page = (strrpos($haystack, $needle) === strlen($haystack) - strlen($needle))? 1 :0;// 追加
			// get remote file
			if (($content = $this->remote_get($url)) && isset($content['body'])) {
				if ($blog_charset === 'UTF-8') {
					$content['body'] = $this->clean_utf8($content['body']);
				}
				$http_code = intval($content['code']);
				switch ($http_code) {
				case 200:
					if ($crawling)
						$this->other_url($content['body'], $url, $http_code, $amp_page);// 第三引数に$amp_pageを追加
				case 404:
					if ($create_404 || $http_code == 200) {
						$content = apply_filters('StaticPress::put_content', $content['body'], $http_code, $amp_page);// 第三引数に$amp_pageを追加
						$this->make_subdirectories($file_dest);
						file_put_contents($file_dest, $content);
						$file_date = date('Y-m-d h:i:s', filemtime($file_dest));
					}
				}
			}
			break;
...................
// 465行目あたり
public function	add_last_modified($content, $http_code = 200, $amp_page) { // 第三引数に$amp_pageを追加
		if (intval($amp_page) === 1) { // APMページの場合の早期リターンを追加
			return $content;
		}
		if (intval($http_code) === 200) {
			$type = preg_match('#<!DOCTYPE html>#i', $content) ? 'html' : 'xhtml';
			switch ( $type ) {
			case 'html':
				$last_modified = sprintf('<meta http-equiv="Last-Modified" content="%s GMT">', gmdate("D, d M Y H:i:s"));
				break;
			case 'xhtml':
			default:
				$last_modified = sprintf('<meta http-equiv="Last-Modified" content="%s GMT" />', gmdate("D, d M Y H:i:s"));
				break;
			}
			$content = preg_replace('#(<head>|<head [^>]+>)#ism', '$1'."\n".$last_modified, $content);
		}
		return $content;
	}

class-static_press.phpへの修正はこれで完了です。

plugin.phpの修正

次に、plugin.php49行目あたりを修正します。

pluign.php
add_filter('StaticPress::get_url', array($staticpress, 'replace_url'));
add_filter('StaticPress::static_url', array($staticpress, 'static_url'));
add_filter('StaticPress::put_content', array($staticpress, 'rewrite_generator_tag'), 10, 2);
add_filter('StaticPress::put_content', array($staticpress, 'add_last_modified'), 10, 3); // 第四引数を2から3に変更
add_filter('StaticPress::put_content', array($staticpress, 'remove_link_tag'), 10, 2);
add_filter('StaticPress::put_content', array($staticpress, 'replace_relative_URI'), 10, 2);
add_filter('https_local_ssl_verify', '__return_false');

これで完了です!!

この修正は許容する引数を2個から3個に変更する、という修正になります。
この修正を行わないと、$amp_pageに引数が渡ってこなくなってしまいます。

確認

あとは、いつも通りに転送を始めれば適用されます。

通常ページ

今まで通り、変わらずに<meta http-equiv="Last-Modified" content="...">が追加されています。

AMPページ

こちらは、<meta http-equiv="Last-Modified" content="...">が削除されているのが確認出来ました。

終わりに

以上のように、ちょっと一手間加えないとStaticPressのAMP対応は完璧にはなりません¥。

とはいえ、そんなに難しい作業ではないので早めにサクッと対応してみてください。