Website construction

Solve the problem that the WP PostViews plug-in does not count after the website is statically cached

Jager · July 13 · 2017 · 5508 read

Suddenly, I found that the article browsing count function failed. It took several months for the article to reach hundreds of views. I thought it was because the recent articles were unpopular and unpopular. However, it has been released for several months, and the number of visitors is less than 200, which is unreasonable. Solve the problem that the WP PostViews plug-in does not count after the website is statically cached

1、 Find problems

So I took time to analyze it, and found that none of the requests for browsing count were found in the website log...

Because the website has opened Static cache (nginx fastcgi_cache), so the counting method of wp postviews will automatically change to the ajax submission method. Normally, the following request records will appear in the Nginx log:

 /wp-admin/admin-ajax.php? postviews_id=xxxx&action=postviews&_=xxxxxxxxxx

However, I read Nginx logs in the last half month and found only a few entries, which seems to be true.

2、 Problem solving

First of all, I opened an article, pressed F12, and then refreshed the page. I searched for the familiar admin ajax in the NetWork content, and found that there was no record, even no request record when searching for php keywords. Searching for keywords directly in the page source code also yielded nothing. It seems that there is really no browse count code.

I thought that the PostViews plug-in did not work because of the WP update, so I opened the WP PostViews source code and found the following logic code:

 if($should_count) {			 if(defined('WP_CACHE') && WP_CACHE) { echo "\n".'<!--  Start Of Script Generated By WP-PostViews -->'. "\n"; 				 echo '<script type="text/javascript">'. "\n"; echo '/* <! [CDATA[ */'."\n"; echo "jQuery.ajax({type:'GET',url:'".admin_url('admin-ajax.php')."',data:'postviews_id=".$id."&action=postviews',cache:false});"; echo '/* ]]> */'. "\n"; echo '</script>'. "\n"; 						 echo '<!--  End Of Script Generated By WP-PostViews -->'. "\n"; } else { if(!update_post_meta($id, 'views', ($post_views+1))) { add_post_meta($id, 'views', 1, true); } } }

Found the necessary condition to enable ajax counting: enable WP_CACHE cache!!!!!

In view of my familiarity with WP, I directly opened the wp-config.php file and found that I commented the following code:

 //define("WP_CACHE", true);

It is estimated that it was commented out when debugging the website.

Then, after uncommenting, reloading php fpm, and clearing the Nginx static cache, the Ajax code familiar to the foreground comes back:

 <!--  Start Of Script Generated By WP-PostViews --> <script type="text/javascript"> /* <! [CDATA[ */ jQuery.ajax({ type:'GET', url:' https://zhang.ge/wp-admin/admin-ajax.php ', data:'postviews_id=5832&action=postviews', cache:false }); /* ]]> */ </script> <!--  End Of Script Generated By WP-PostViews -->

Take a look at the Nginx log, admin-ajax.php? The request of xxx has also returned. It seems that the browsing count function has returned to normal.

3、 Conclusion analysis

① Why is it not that we do not count at all or only count once?

After tracing back to the next process, it is obvious that there are still counts after the article is published, but the count is very small. Why? In fact, the reason is very simple. When the article is cached for the first time, WP PostViews actually works once, using the php count in a non cached environment. After the count, the article will be cached, and the count will not be updated when it is accessed again. The browse count will not be initiated again until someone makes a comment or the cache expires, causing the cache to be refreshed! This is why we do not count or only count once.

② Counting conditions in the WP PostViews cache environment

This problem is very common. I just searched it and found that there are many similar situations. That is to say, the PostViews plug-in will judge whether WP has enabled caching (WP_CACHE). If it is enabled, it will use the ajax counting method. Otherwise, it will use the php counting method.

Therefore, if you use a non PHP caching mechanism, such as Nginx's fastcgi_cache or proxy_cahe, you must enable WP_CACHE in wp-config.php:

 define("WP_CACHE", true);

Let the plug-in know that your website has a caching mechanism. Otherwise, you have to modify the plug-in to remove this judgment and force the plug-in to insert Ajax counting code into the page.

30 responses
  1. Acoustic imager 2017-7-14 · 11:20

    The article is well written and taught.

  2. Minor Five 2017-7-14 · 11:42

    Thank God for his dry goods, and I learned a lot from him. thank you

  3. Long Xiaotian 2017-7-14 · 15:42

    It seems to refresh the page, but the count has not changed~
    My CDN is enabled, and the count is not refreshed......: sad:

    •  avatar
      Jager 2017-7-15 · 14:15

      Just change the backstage

  4. The moon climbs the stairs 2017-7-14 · 17:48

    I have disabled displaying the number of front end article views! Since then, I feel much more comfortable!

    •  avatar
      Jager 2017-7-15 · 14:15

      This can have

  5. Long Xiaotian 2017-7-15 · 19:00

    : shock: It's the "background change is OK" [Stunned]

  6. Freedom House Blog 2017-7-16 · 13:15

    After learning, it's the same thing. The skills are different! I met this problem before, but it was over! Forwarded! thank you

  7. Brother Jiu 2017-7-16 · 15:14

    Worship the great god of technology. If such a white man like me encounters this problem, he will have to go to Baidu, which may not solve it!

  8. BanYuner 2017-7-16 · 19:23

    As long as the number in the database is changing

  9. Grave dynamic balancer 2017-7-17 · 8:48

    The article is well written.

  10. Xiaozhuang 2017-7-17 · 11:36

    Forcibly insert a wave of messages

  11. Ktv training 2017-7-18 · 11:13

    Well, that's not bad

  12. Chenghang Xiansen 2017-7-18 · 16:02

    It has always been thought that the statistics of foreground views can also change in real time, but it can't be solved. Today, I thought that God finally paid attention to this point...

  13. Xiaoning in a trance 2017-7-19 · 17:41

    I wonder if Jager is interested in posting an article about the limit of the number of IP requests at the same time?
    The articles on the network are very vague, and the operation is very complicated. I'm afraid of injuring spiders by mistake. After all, hard SEO results are not easy.
    But for some BT level IP, it is deadly to request 10 to 20 times per second at the same time.
    Hey hey, you can turn off the vibration, my mother~~I type with my eyes closed.

  14. US stock market 2017-7-22 · 23:24

    pretty good! TKS~~~~~ :evil:

  15. Yizhi Blog 2017-7-25 · 9:51

    After being taught, let's learn cool:

  16. Internet Notes 2017-7-25 · 16:33

    Very practical, thanks for sharing~

  17. Nanxia 2017-7-29 · 0:58

    Is it not enough to set the periodic refresh time cycle of the CDN a little shorter?

  18. Corduroy fabric 2017-8-3 · 8:53

    It's very easy to use, step by step

  19. Online earning 2017-8-5 · 21:36

    Good Good, learned

  20. I also shared a solution for this problem before, which is to automatically increase the number of views by sending a request to the background through Ajax after the page is loaded. Solve the problem that the number of article views does not increase automatically after WordPress starts CDN caching

    •  avatar
      Jager 2017-8-8 · 12:54

      Niu B, you can change the code again to support repeated IP access to the same page and repeated counting. It may be better to introduce Redis or Memcached cache to record the IP status. For example, if one IP accesses the same page multiple times a day, only one visit is counted.

  21. Reading novels online 2017-8-22 · 14:47

    My cache hasn't been fixed

  22. yimo 2017-8-23 · 11:13

    After the CDN user logs in, the login box of the current page is still not logged in. It seems that this problem cannot be solved, so the html cache has to be closed

  23. I want to know which plugin WordPress automatically adds internal links to.

  24. Jinan SEO 2017-9-3 · 21:29

    My blog has been using WP PostViews all the time. I collected it first, and then I will follow it if I can use it later. Thanks for sharing

  25. Old Ghost 2017-9-12 · 15:24

    It took one day to solve this problem. Thank you, blogger.

  26. Xiaobai 2018-4-10 · 20:41

    I want to ask, what should I do if I use CDN instead of counting? I see that the blogger's articles use CDN, and they don't count