解决WordPress开启CDN缓存后文章浏览量不自增的问题

wordpress admin 3个月前 (05-27) 17次浏览 0个评论
文章目录[隐藏]

WordPress开启CDN静态缓存页面之后,当我们访问文章的时候,便不会去请求源站,而是返回CDN缓存了的页面,所以文章的浏览数量不会进行自增以及其他的变化。

解决WordPress开启CDN缓存后文章浏览量不自增的问题
解决WordPress开启CDN缓存后文章浏览量不自增的问题

解决办法

解决这个问题,我们可以通过Ajax来异步请求服务器,然后再进行文章的浏览数量自增或其他的操作。(注:本方法仅适用于使用了WP-PostViews插件或者和具有WP-PostViews相同字段的博客,若你的文章统计字段不同,可自行修改下方SQL语句以达到效果)
首先,我们在网站的根目录新建一个操作文件,命名为views.php,将以下代码写入此文件中。
根据@张戈博主提供的建议与信息,将浏览量记录操作做了过滤以及部分优化,但是由于在服务器端进行验证的话如果不使用Redis等缓存,操作量以及存储的Cookie数据可能会比较多,于是我换了一种方式来进行验证过滤(JavaScript部分),代码如下:

<ol class="linenums"><li class="L0"><span class="pln"> </span><span class="kwd">require</span><span class="pun">(</span><span class="str">'../../wp-blog-header.php'</span><span class="pun">);</span></li><li class="L1"><span class="pln"> $isXhr </span><span class="pun">=</span><span class="pln"> $_SERVER</span><span class="pun">[</span><span class="str">'HTTP_X_REQUESTED_WITH'</span><span class="pun">];</span></li><li class="L2"><span class="pln"> </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="pln">strtoupper</span><span class="pun">(</span><span class="pln">$isXhr</span><span class="pun">)</span><span class="pln"> </span><span class="pun">==</span><span class="pln"> strtoupper</span><span class="pun">(</span><span class="str">"XMLHttpRequest"</span><span class="pun">))</span><span class="pln"> </span><span class="pun">{</span></li><li class="L3"><span class="pln"> $post_id </span><span class="pun">=</span><span class="pln"> $_POST</span><span class="pun">[</span><span class="str">'post_id'</span><span class="pun">];</span></li><li class="L4"><span class="pln"> $po </span><span class="pun">=</span><span class="pln"> get_post_meta</span><span class="pun">(</span><span class="pln">$post_id</span><span class="pun">,</span><span class="str">'views'</span><span class="pun">,</span><span class="kwd">true</span><span class="pun">);</span></li><li class="L5"><span class="pln"> </span><span class="kwd">if</span><span class="pun">(!</span><span class="pln">$po</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span></li><li class="L6"><span class="pln"> </span><span class="com">//表示不存在这个自定义栏目,那么新增这个自定义栏目</span></li><li class="L7"><span class="pln"> add_post_meta</span><span class="pun">(</span><span class="pln">$post_id</span><span class="pun">,</span><span class="pln"> </span><span class="str">'views'</span><span class="pun">,</span><span class="pln"> </span><span class="lit">1</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">true</span><span class="pun">);</span></li><li class="L8"><span class="pln"> echo </span><span class="lit">1</span><span class="pun">;</span></li><li class="L9"><span class="pln"> </span><span class="pun">}</span><span class="pln"> </span><span class="kwd">else</span><span class="pln"> </span><span class="pun">{</span></li><li class="L0"><span class="pln"> </span><span class="com">// 若已存在这个自定义栏目,则先获取当前数值,并递增+1,再更新数据库</span></li><li class="L1"><span class="pln"> $count </span><span class="pun">=</span><span class="pln"> $po </span><span class="pun">+</span><span class="pln"> </span><span class="lit">1</span><span class="pun">;</span></li><li class="L2"><span class="pln"> update_post_meta</span><span class="pun">(</span><span class="pln">$post_id</span><span class="pun">,</span><span class="str">'views'</span><span class="pun">,</span><span class="pln">$count</span><span class="pun">);</span></li><li class="L3"><span class="pln"> echo $count</span><span class="pun">;</span></li><li class="L4"><span class="pln"> </span><span class="pun">}</span></li><li class="L5"><span class="pln"> </span><span class="pun">}</span><span class="pln"> </span><span class="kwd">else</span><span class="pln"> </span><span class="pun">{</span></li><li class="L6"><span class="pln"> header</span><span class="pun">(</span><span class="str">"Status: 403 Not Found"</span><span class="pun">);</span></li><li class="L7"><span class="pln"> </span><span class="kwd">exit</span><span class="pun">;</span></li><li class="L8"><span class="pln"> </span><span class="pun">}</span></li></ol>

接着我们在主题的page.php文件中插入以下代码:

<ol class="linenums"><li class="L0"><span class="pln">$</span><span class="pun">(</span><span class="kwd">function</span><span class="pun">(){</span></li><li class="L1"><span class="pln"> </span><span class="kwd">function</span><span class="pln"> page_views</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span></li><li class="L2"><span class="pln"> </span><span class="kwd">if</span><span class="pun">(!</span><span class="pln">window</span><span class="pun">.</span><span class="pln">localStorage</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span></li><li class="L3"><span class="pln"> console</span><span class="pun">.</span><span class="pln">info</span><span class="pun">(</span><span class="str">'浏览器不支持localStorage'</span><span class="pun">);</span></li><li class="L4"><span class="pln"> </span><span class="pun">}</span><span class="kwd">else</span><span class="pun">{</span></li><li class="L5"><span class="pln"> </span><span class="kwd">var</span><span class="pln"> post_id </span><span class="pun">=</span><span class="pln"> </span><span class="pun"><?</span><span class="pln">php the_ID</span><span class="pun">();?>;</span></li><li class="L6"><span class="pln"> </span><span class="kwd">var</span><span class="pln"> views </span><span class="pun">=</span><span class="pln"> localStorage</span><span class="pun">.</span><span class="pln">getItem</span><span class="pun">(</span><span class="str">"view_list"</span><span class="pun">);</span></li><li class="L7"><span class="pln"> </span><span class="kwd">if</span><span class="pun">(</span><span class="pln">views</span><span class="pun">===</span><span class="kwd">null</span><span class="pun">){</span></li><li class="L8"><span class="pln"> localStorage</span><span class="pun">.</span><span class="pln">setItem</span><span class="pun">(</span><span class="str">"view_list"</span><span class="pun">,</span><span class="pln">post_id</span><span class="pun">);</span></li><li class="L9"><span class="pln"> $</span><span class="pun">.</span><span class="pln">post</span><span class="pun">(</span><span class="str">"/views.php?time="</span><span class="pun">+</span><span class="kwd">new</span><span class="pln"> </span><span class="typ">Date</span><span class="pun">().</span><span class="pln">getTime</span><span class="pun">(),{</span><span class="pln">post_id</span><span class="pun">:</span><span class="pln">post_id</span><span class="pun">});</span></li><li class="L0"><span class="pln"> </span><span class="kwd">return</span><span class="pln"> </span><span class="kwd">true</span><span class="pun">;</span></li><li class="L1"><span class="pln"> </span><span class="pun">}</span><span class="kwd">else</span><span class="pun">{</span></li><li class="L2"><span class="pln"> </span><span class="kwd">var</span><span class="pln"> item </span><span class="pun">=</span><span class="pln"> views</span><span class="pun">.</span><span class="pln">split</span><span class="pun">(</span><span class="str">"|"</span><span class="pun">);</span></li><li class="L3"><span class="pln"> </span><span class="kwd">var</span><span class="pln"> </span><span class="kwd">bool</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> $</span><span class="pun">.</span><span class="pln">inArray</span><span class="pun">(</span><span class="pln">post_id</span><span class="pun">+</span><span class="str">''</span><span class="pun">,</span><span class="pln">item</span><span class="pun">);</span></li><li class="L4"><span class="pln"> </span><span class="kwd">if</span><span class="pun">(</span><span class="kwd">bool</span><span class="pun">===-</span><span class="lit">1</span><span class="pun">){</span></li><li class="L5"><span class="pln"> $</span><span class="pun">.</span><span class="pln">post</span><span class="pun">(</span><span class="str">"/views.php?time="</span><span class="pun">+</span><span class="kwd">new</span><span class="pln"> </span><span class="typ">Date</span><span class="pun">().</span><span class="pln">getTime</span><span class="pun">(),{</span><span class="pln">post_id</span><span class="pun">:</span><span class="pln">post_id</span><span class="pun">});</span></li><li class="L6"><span class="pln"> localStorage</span><span class="pun">.</span><span class="pln">setItem</span><span class="pun">(</span><span class="str">"view_list"</span><span class="pun">,</span><span class="pln">views</span><span class="pun">+</span><span class="str">"|"</span><span class="pun">+</span><span class="pln">post_id</span><span class="pun">);</span></li><li class="L7"><span class="pln"> </span><span class="kwd">return</span><span class="pln"> </span><span class="kwd">true</span><span class="pun">;</span></li><li class="L8"><span class="pln"> </span><span class="pun">}</span></li><li class="L9"><span class="pln"> </span><span class="kwd">return</span><span class="pln"> </span><span class="kwd">false</span><span class="pun">;</span></li><li class="L0"><span class="pln"> </span><span class="pun">}</span></li><li class="L1"><span class="pln"> </span></li><li class="L2"><span class="pln"> </span><span class="pun">}</span></li><li class="L3"><span class="pln"> </span><span class="pun">}</span></li><li class="L4"><span class="pln"> page_views</span><span class="pun">();</span></li><li class="L5"><span class="pun">});</span></li></ol>

到这一步,就已经成功解决了这个问题。

解决WordPress开启CDN缓存后文章浏览量不自增的问题
故事里的归途
落叶归根才是最终的归宿地
查看


免费部落 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:解决WordPress开启CDN缓存后文章浏览量不自增的问题
喜欢 (0)
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址