• 武汉加油,中国加油,免费投稿邮箱 creust@creust.com,vps,服务器,vps 推荐

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

VPS推荐 创启云客 5个月前 (06-16) 2058次浏览 已收录 0个评论 扫描二维码

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

解决办法

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

 <span class="kwd">require</span><span class="pun">(</span><span class="str">'../../wp-blog-header.php'</span><span class="pun">);</span><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>
 <span class="kwd">if</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="pun">==</span><span class="pln"> strtoupper</span><span class="pun">(</span><span class="str">"XMLHttpRequest"</span><span class="pun">))</span> <span class="pun">{</span><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><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>
 <span class="kwd">if</span><span class="pun">(!</span><span class="pln">$po</span><span class="pun">)</span> <span class="pun">{</span>
 <span class="com">//表示不存在这个自定义栏目,那么新增这个自定义栏目</span><span class="pln">
 add_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="lit">1</span><span class="pun">,</span> <span class="kwd">true</span><span class="pun">);</span><span class="pln">
 echo </span><span class="lit">1</span><span class="pun">;</span>
 <span class="pun">}</span> <span class="kwd">else</span> <span class="pun">{</span>
 <span class="com">// 若已存在这个自定义栏目,则先获取当前数值,并递增+1,再更新数据库</span><span class="pln">
 $count </span><span class="pun">=</span><span class="pln"> $po </span><span class="pun">+</span> <span class="lit">1</span><span class="pun">;</span><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><span class="pln">
 echo $count</span><span class="pun">;</span>
 <span class="pun">}</span>
 <span class="pun">}</span> <span class="kwd">else</span> <span class="pun">{</span><span class="pln">
 header</span><span class="pun">(</span><span class="str">"Status: 403 Not Found"</span><span class="pun">);</span>
 <span class="kwd">exit</span><span class="pun">;</span>
 <span class="pun">}</span>

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

<span class="pln">$</span><span class="pun">(</span><span class="kwd">function</span><span class="pun">(){</span>
 <span class="kwd">function</span><span class="pln"> page_views</span><span class="pun">()</span> <span class="pun">{</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="pun">{</span><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>
 <span class="pun">}</span><span class="kwd">else</span><span class="pun">{</span>
 <span class="kwd">var</span><span class="pln"> post_id </span><span class="pun">=</span> <span class="pun"><?</span><span class="pln">php the_ID</span><span class="pun">();?>;</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>
 <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><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><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="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>
 <span class="kwd">return</span> <span class="kwd">true</span><span class="pun">;</span>
 <span class="pun">}</span><span class="kwd">else</span><span class="pun">{</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>
 <span class="kwd">var</span> <span class="kwd">bool</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>
 <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><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="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><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>
 <span class="kwd">return</span> <span class="kwd">true</span><span class="pun">;</span>
 <span class="pun">}</span>
 <span class="kwd">return</span> <span class="kwd">false</span><span class="pun">;</span>
 <span class="pun">}</span>
 
 <span class="pun">}</span>
 <span class="pun">}</span><span class="pln">
 page_views</span><span class="pun">();</span>
<span class="pun">});</span>

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

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

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

 

若方法中存在 BUG 请在下方评论中陈述问题,谢谢!


创启云客 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:解决WordPress开启CDN缓存后文章浏览量不自增的问题
喜欢 (0)
[13738388032@qq.com]
分享 (0)
创启云客
关于作者:
vps,服务器,云服务器,第三方支付,免费CDN,免费pos,Hexo,pos机,支付圈,免费vps,Discuz,emlog,主题,日本vps,国外服务器,美国vps,香港vps,便宜vps,便宜服务器,vps服务器
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

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

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