<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Soul Of Free Loop &#187; AMP for WP</title>
	<atom:link href="https://zohead.com/archives/tag/amp-for-wp/feed" rel="self" type="application/rss+xml" />
	<link>https://zohead.com</link>
	<description>Uranus Zhou&#039;s Blog</description>
	<lastBuildDate>Sat, 19 Jul 2025 15:42:46 +0000</lastBuildDate>
	<language>zh-CN</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.8</generator>
	<item>
		<title>WordPress 3.8开启Google AMP的问题</title>
		<link>https://zohead.com/archives/wordpress-3-amp/</link>
		<comments>https://zohead.com/archives/wordpress-3-amp/#comments</comments>
		<pubDate>Tue, 01 Aug 2017 17:37:56 +0000</pubDate>
		<dc:creator><![CDATA[Uranus Zhou]]></dc:creator>
				<category><![CDATA[Google]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[AMP]]></category>
		<category><![CDATA[AMP for WP]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[qTranslate]]></category>
		<category><![CDATA[插件]]></category>

		<guid isPermaLink="false">https://zohead.com/?p=1487</guid>
		<description><![CDATA[关于 AMP Accelerated Mobile Pages（简称 AMP）是 Google 带领开发的开源项目，主要为了提升移动设备对网站的访问速度，大概包括 AMP HTML、AMP JS 和 AMP Cache 三部分，非常适合博客及新闻展示类的静态网站。 有关 AMP 的详细介绍可以参考其官方网站： https://www.ampproject.org/zh_cn/ AMP 网页只能使用 AMP HTML 中允许的有限的标签，不允许使用 AMP Runtime、AMP Components 之外的 JS，而且 AMP Runtime、Components 都必须从 cdn.amppr [&#8230;]]]></description>
				<content:encoded><![CDATA[<h2 id="about-amp">关于 AMP</h2>
<p><a href="https://en.wikipedia.org/wiki/Accelerated_Mobile_Pages" target="_blank">Accelerated Mobile Pages</a>（简称 AMP）是 Google 带领开发的开源项目，主要为了提升移动设备对网站的访问速度，大概包括 <strong>AMP HTML</strong>、<strong>AMP JS</strong> 和 <strong>AMP Cache</strong> 三部分，非常适合博客及新闻展示类的静态网站。</p>
<p>有关 AMP 的详细介绍可以参考其官方网站：</p>
<p><a href="https://www.ampproject.org/zh_cn/" target="_blank">https://www.ampproject.org/zh_cn/</a></p>
<p>AMP 网页只能使用 AMP HTML 中允许的有限的标签，不允许使用 AMP Runtime、AMP Components 之外的 JS，而且 AMP Runtime、Components 都必须从 <code>cdn.ampproject.org</code> 加载，因此目前 AMP 网页都必须在翻墙状态下才能正常浏览。</p>
<p>AMP Cache 是负责缓存并传输 AMP 页面的 CDN 服务器，用户在 Google 移动端中点击 AMP 网站时，实际访问的是缓存过的页面。</p>
<h2 id="wordpress-amp-problem">WordPress 启用 AMP 的问题</h2>
<p>WordPress 官方提供了 <a href="https://wordpress.org/plugins/amp/" target="_blank">AMP</a> 插件以支持直接为博客开启 AMP 加速，只是官方插件要求 WordPress 最低为 4.4 版本。</p>
<p>由于我的博客安装了 <a href="http://www.qianqin.de/qtranslate/" target="_blank">qTranslate</a> 这款多语言插件，而该插件却只支持到 WordPress 3.8.1 版本，如果升级 WordPress 到最新版本，qTranslate 插件就会自动禁用。然而 qTranslate 插件是对 WordPress 数据库直接进行修改的，禁用之后博客文章就可能会同时显示多种语言的内容，这样显然是不可接受的。</p>
<p>虽然网上已经有人继承 qTranslate 原作发布了例如 qTranslate X 之类的支持新版本 WordPress 的插件，但为了数据安全（其实是懒）考虑我暂时还是不打算直接升级 WordPress 了。</p>
<p>不过还好我在 WordPress 插件库里找到一款看起来支持 WordPress 3.8 的 AMP 插件：<a href="https://wordpress.org/plugins/accelerated-mobile-pages/" target="_blank">AMP for WP</a>，毕竟看此插件的介绍和评分都比官方的 AMP 插件要强哦，而且最低只要求 WordPress 3.0 版本。</p>
<p>AMP for WP 安装起来还是非常顺利的，插件选项里目前基本只提供了 Google Analytics UID 的设置选项，启用之后访问 AMP 版网站的方法是在地址后面加上 <code>?wpamp</code> 后缀。</p>
<p>不过我刚开始测试访问 AMP 首页和文章就碰到问题了，直接没有任何显示了，看网页源代码输出信息不全，显然是 AMP for WP 插件对老版本 WordPress 兼容性还是有点问题。</p>
<h2 id="mod-amp-for-wp">修改 AMP for WP</h2>
<p>首先修改 AMP for WP 插件的 <code>wp-amp-ninja/admin/common.php</code> 文件，打开 PHP 报错开关：</p>
<pre class="brush: php; title: wp-amp-ninja/admin/common.php; notranslate">
error_reporting(1);
</pre>
<p>这下访问博客首页和文章页面就能看到详细报错了：</p>
<pre class="brush: plain; title: ; notranslate">
Fatal error: Call to undefined function get_the_permalink() in /www/zohead.com/wp-content/plugins/wp-amp-ninja/includes/wpamp-front.php on line 24

Fatal error: Call to undefined function the_posts_pagination() in /www/zohead.com/wp-content/plugins/wp-amp-ninja/template/wpamp-content.php on line 53


Fatal error: Call to undefined function the_archive_title() in /www/zohead.com/wp-content/plugins/wp-amp-ninja/includes/wpamp-functions.php on line 600

Fatal error: Call to undefined function the_archive_description() in /www/zohead.com/wp-content/plugins/wp-amp-ninja/includes/wpamp-functions.php on line 601
</pre>
<p>经过查阅 WordPress 官方文档发现：</p>
<ol>
<li><a href="https://developer.wordpress.org/reference/functions/get_the_permalink/" target="_blank">get_the_permalink</a> 函数在 WordPress 3.9.0 版本开始引入，其实就是 1.0.0 版本就已经有的 <a href="https://developer.wordpress.org/reference/functions/get_permalink/" target="_blank">get_permalink</a> 函数；</li>
<li><a href="https://developer.wordpress.org/reference/functions/the_posts_pagination/" target="_blank">the_posts_pagination</a> 函数在 WordPress 4.1.0 版本开始引入，一同引入的还有 <code>the_posts_navigation</code> 函数，与 <code>get_the_permalink</code> 函数一样都位于 WordPress 代码库的 <code>wp-includes/link-template.php</code> 文件；</li>
<li>至于 <a href="https://developer.wordpress.org/reference/functions/the_archive_title/" target="_blank">the_archive_title</a> 和 <a href="https://developer.wordpress.org/reference/functions/the_archive_description" target="_blank">the_archive_description</a> 函数的功能比较类似，支持显示文章的标题和描述并在前后插入自定义代码，同样也在 WordPress 4.1.0 版本开始引入，位于 WordPress 代码库的 <code>wp-includes/general-template.php</code> 文件。</li>
</ol>
<p>由此可见，AMP for WP 实际上应该至少要求 WordPress 4.1.0 版本，只是作者并没有测试过是否兼容较低版本的 WordPress。</p>
<p>不过还好我看了下 WordPress 最新 4.8 版本的代码，上面缺少的几个 WordPress 内置函数都是功能比较独立的。</p>
<p>因此我稍微修改了一下博客目录下的 <code>link-template.php</code> 和 <code>general-template.php</code> 文件，将最新版本的 <code>get_the_permalink</code>、<code>the_posts_pagination</code>、<code>the_posts_navigation</code>、<code>the_archive_title</code>、<code>the_archive_description</code> 函数实现分别 Backport 到 WordPress 3.8 版本。Backport 完成之后再访问博客首页和文章，就没有 PHP 报错了。</p>
<p>不过初步试用下来，我还是发现 AMP for WP 插件的一些小问题：</p>
<ol>
<li>如果通过 qTranslate 插件访问博客的英文版本（通过增加 <code>?lang=en</code> 地址后缀），对应的 AMP 版页面地址会不正确，AMP for WP 插件在转换地址时并没有考虑地址中会带有其它后缀的情况；</li>
<li>AMP for WP 对于带有多种语言标题的文章直接显示数据库中多种语言合并在一起的标题效果，而并不是 qTranslate 插件根据客户端语言自动处理后的效果，这个问题在我使用的几个 WordPress 主题中都不会出现；</li>
<li>文章中插入的多行代码被合并成一行显示了，这是明显不能接受的；</li>
<li>文章的中文发表时间无法正确显示，统一显示为当前日期。</li>
</ol>
<p>第一个问题需要修改 AMP for WP 插件的 <code>wpamp-front.php</code>、<code>wpamp-functions.php</code>、<code>wpamp-header.php</code> 这几个文件中转换地址的代码，例如 <code>wpamp-front.php</code> 文件的修改：</p>
<pre class="brush: diff; title: wpamp-front.php; notranslate">
 		$the_permalink = get_the_permalink( $post-&gt;ID );
 	}
 	
-	$the_permalink = rtrim( $the_permalink, '/' ) . '/';
+	$the_permalink = rtrim( $the_permalink, '/' );
 	$perma = strpos( $the_permalink, &quot;?&quot; );
 	if ( $perma === false ) {
+		$the_permalink .= '/';
 		$sConnector = &quot;?&quot;;
 	} else {
 		$sConnector = &quot;&amp;&quot;;
</pre>
<p>第二个问题需要修改 <code>wpamp-content.php</code> 文件，文章标题要做 <code>apply_filters</code> 处理：</p>
<pre class="brush: diff; title: wpamp-content.php; notranslate">
                 &lt;div id=&quot;amp-pagination&quot;&gt;
                 	&lt;?php $prev_post = get_previous_post(); if (!empty( $prev_post )): ?&gt;
-                    	&lt;div class=&quot;prev&quot;&gt;&lt;a href=&quot;&lt;?php echo wp_amp_permalink($prev_post-&gt;ID, AMP_CONSTANT); ?&gt;&quot;&gt; &amp;laquo; &lt;?php echo $prev_post-&gt;post_title ?&gt;&lt;/a&gt;&lt;/div&gt;
+                    	&lt;div class=&quot;prev&quot;&gt;&lt;a href=&quot;&lt;?php echo wp_amp_permalink($prev_post-&gt;ID, AMP_CONSTANT); ?&gt;&quot;&gt; &amp;laquo; &lt;?php echo apply_filters( 'the_title', $prev_post-&gt;post_title, $prev_post-&gt;ID ); ?&gt;&lt;/a&gt;&lt;/div&gt;
 					&lt;?php endif ?&gt;
 					&lt;?php  $next_post = get_next_post(); if (!empty( $next_post )): ?&gt;
-                        &lt;div class=&quot;next&quot;&gt;&lt;a href=&quot;&lt;?php echo wp_amp_permalink($next_post-&gt;ID, AMP_CONSTANT); ?&gt;&quot;&gt;&lt;?php echo $next_post-&gt;post_title ?&gt; &amp;raquo; &lt;/a&gt;&lt;/div&gt;
+                        &lt;div class=&quot;next&quot;&gt;&lt;a href=&quot;&lt;?php echo wp_amp_permalink($next_post-&gt;ID, AMP_CONSTANT); ?&gt;&quot;&gt;&lt;?php echo apply_filters( 'the_title', $next_post-&gt;post_title, $next_post-&gt;ID ); ?&gt; &amp;raquo; &lt;/a&gt;&lt;/div&gt;
 					&lt;?php endif ?&gt;
                     &lt;div class=&quot;clearfix&quot;&gt;&lt;/div&gt;
                 &lt;/div&gt;
</pre>
<p>第三个问题就比较简单了，将 <code>simple_html_dom.php</code> 文件中替换回车和换行符的处理去掉就可以了。</p>
<p>第四个文章发表时间的问题也可以修改 <code>wpamp-content.php</code> 文件简化处理：</p>
<pre class="brush: diff; title: wpamp-content.php; notranslate">
                     &lt;?php endif; ?&gt;
                     By &lt;span class=&quot;amp-wp-author&quot;&gt;&lt;a href=&quot;&lt;?php echo wp_amp_permalink( NULL, AMP_CONSTANT, get_author_posts_url( get_the_author_meta( 'ID' ) ) ); ?&gt;&quot;&gt;&lt;?php echo esc_html( get_the_author() ); ?&gt;&lt;/a&gt;&lt;/span&gt;
-                    &lt;?php $the_time = date_i18n( get_option( 'date_format' ), strtotime( get_the_time('F jS, Y') ) ); ?&gt;
-                    on &lt;time datetime=&quot;&lt;?php echo $the_time; ?&gt;&quot;&gt;&lt;?php echo $the_time; ?&gt;&lt;/time&gt;
+                    on &lt;time datetime=&quot;&lt;?php echo get_the_date('c'); ?&gt;&quot;&gt;&lt;?php echo get_the_date('c'); ?&gt;&lt;/time&gt;
                 &lt;/li&gt;
</pre>
<p>另外我还对 AMP for WP 插件的页脚处理做了点小修改，将页脚上切换到博客首页桌面版本的链接改为切换到当前文章的桌面版本，方便访问者使用。</p>
<h2 id="summary">总结</h2>
<p>对 AMP for WP 插件修改完成之后就可以访问博客的 AMP 版进行验证了，例如我的博客的 AMP 版首页地址就是：</p>
<p><a href="https://zohead.com/?wpamp" target="_blank">https://zohead.com/?wpamp</a></p>
<p>普通用户可以通过安装 Google 推出的 <a href="https://chrome.google.com/webstore/detail/amp-validator/nmoffdblmcmgeicmolmhobpoocbbmknc" target="_blank">AMP Validator</a> 官方 Chrome 扩展检测所访问的 AMP 站点是否配置正确。</p>
<p>网站所有者可以用 Google 搜索的 AMP 在线测试工具进行检测：</p>
<p><a href="https://search.google.com/test/amp" target="_blank">https://search.google.com/test/amp</a></p>
<p>输入要测试的 AMP 版网址就可以开始检测，另外这个测试工具的另一个好处就是检测通过之后可以直接把你的 AMP 站点提交给 Google，让 Google 搜索蜘蛛尽快收录并进行 CDN 缓存。</p>
<p>现在通过 Google 移动端（例如 Android Chrome 浏览器）搜索我的博客，应该就能在搜索结果里看到下面的 AMP 闪电标志了：</p>
<p><img src="http://res.cloudinary.com/digwht2y0/image/upload/v1737371102/google-search-amp.jpg" alt="Google 搜索结果 AMP 标志" title="Google 搜索结果 AMP 标志"></p>
<p>点击搜索结果，你会发现访问的其实是 Google 缓存过的 AMP 地址，下面地址只有移动端浏览器才会显示 AMP 网页：</p>
<p><a href="https://www.google.com/amp/s/zohead.com/%3fwpamp" target="_blank">https://www.google.com/amp/s/zohead.com/%3fwpamp</a></p>
<p>初步启用 AMP 的大体感受就是如果你的梯子还可以，那访问 Google 缓存过的 AMP 网站速度还是非常快的，我这里估计访问 AMP 版网站所需要的流量大概为桌面版本的 1/3。</p>
<p>后面有时间的话还是希望能对 AMP for WP 插件继续改进，例如支持其它的 AMP 后缀或者使用专门的 AMP 子域名方便自己的服务器进行缓存之类的，最后祝大家玩的开心。</p>
]]></content:encoded>
			<wfw:commentRss>https://zohead.com/archives/wordpress-3-amp/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
