Count post views when cache is available

In the article on how to do the view count function, at the end of the article I said, most of the plugins or things related to counting the views of the article cannot work if the website is using the HTML Cache form ( compress the content of the page into an HTML file on the hard drive and retrieve) because then the website will not have a PHP request sent to the server, but simply view a static HTML content.

There is another solution to use the post/page view count feature and still work when the website is cached is use AJAX. AJAX is an asynchronous loading technique of Javascript and XML, so whether the website has a cache or not, the sending/receiving data related to the view will still be sent to the server after the page has finished loading, so it will work when there is a link. cache.wp-ajax

While I was planning to write a tutorial on how to improve this article’s code into AJAX powered view counting, I discovered that WordPress now has a free plugin with similar functionality, even more than name is AJAX Hits Counter + Popular Posts Widget. As the name suggests, it will count visitors’ content views using AJAX and support a highly viewed post list widget.

Customize AJAX Hits Counter's most viewed post widget

Customize AJAX Hits Counter’s most viewed post widget

What I like about this plugin is that it is highly customizable in the widget. Specifically, in the widget you can customize the widget’s cache time (default is 3600 seconds, it will update once) and how to display the list of posts on the widget when you can customize the structure and CSS.

Count post views when cache 15

Immediately after installing the plugin, it only has the task of counting views, not displaying it. So if you want, you can insert the following code inside the single.php or content.php template to display the number of views of each post.

<?php echo( ajax_hits_counter_get_hits(get_the_ID()) ); ?>

After testing, I think this plugin works quite well at counting views with cached websites and can completely work with popular cache plugins like WP Super Cache, W3 Total Cache and Quick Cache.

See more:  Write articles by series with the Organize Series plugin

Good news for those who are using the plugin WP-PostViews to be can import view data from WP-PostViews to AJAX Hits Counter in the Tools menu.

ajax-hit-counter-import

Count post views when cache 15

But the function to display the number of views of the article is not handled by AJAX, but receives data manually. Therefore, when the website has a cache, you should not use the display view function because it will display incorrectly, now the author has confirmed that this function will be included in the latest update ((See at https://wordpress.org/support/topic/retrieve-hits-count-with-ajax?replies=4)).

Even so, this plugin is still a very good “lifesaver” for users who need to count visitor’s post views but are concerned about the problem of using HTML Cache plugins, and I hope in the future. In the future, this plugin will update the feature of displaying external views by AJAX to be more complete.


Source: Count post views when cache is available
– TechtipsnReview

, , ,

Leave a Reply

Your email address will not be published. Required fields are marked *