Solving the Problem of the Postviews Plugin's View Count Cache Not Refreshing
A long, long time ago (back when I had just set up the site), I started using the WP Postviews plugin. Since I had enabled a CDN at the time, it couldn't count visits, so I wrote the article「Page Views Don't Increase After Enabling a CDN in WordPress」.
Now I've enabled HTML caching again. Although the backend can count visits, the frontend page still doesn't refresh. There was nothing I could do but search everywhere for tutorials. Later, I found a perfect solution at 月宅酱, so I'm sharing it with everyone.
Solution
Step 1: Enter the plugin directory /wp-content/plugins/wp-postviews,
open the postviews-cache.js file, delete all the code inside it, add the following code, and press ctrl+s to save.
jQuery.ajax({
type: "GET",
url: viewsCacheL10n.admin_ajax_url,
data: "postviews_id=" + viewsCacheL10n.post_id + "&action=postviews",
cache: !1
});
jQuery(document).ready(function() {
var ajax_data = {
action: "show_postview",
bigfa_view: viewsCacheL10n.post_id
};
$.post(viewsCacheL10n.admin_ajax_url, ajax_data,
function(data) {
$('.show-view').html(data);
});
return false;
});
Step 2: Open the wp-postviews.php file and add the following code at the end
add_action('wp_ajax_nopriv_show_postview', 'show_postview');
add_action('wp_ajax_show_postview', 'show_postview');
function show_postview(){
$views_options = get_option('views_options');
$ID = $_POST["bigfa_view"];
$custom_fields = get_post_custom($ID);
$my_custom_field = $custom_fields['views'];
foreach ( $my_custom_field as $key => $value ) {
echo str_replace('%VIEW_COUNT%', number_format_i18n($value), $views_options['template']);
}
die;
}
Good-looking people have already solved it perfectly, while ugly people are still struggling desperately—wahahaha!
Comments
(0)No comments yet. Start the conversation.