How To: Updating the views count via AJAX
-
This is a follow-up / update to “[Plugin: WordPress Popular Posts] using the jQuery load() method”.
Some themes use AJAX to retrieve and display posts and pages. In these conditions WPP won’t be able to update the views count of these posts because the single.php template is not loaded by the theme.
To work around that, you can send a POST request to WordPress via AJAX once your AJAX script has been executed to tell WPP to update the views count. For example:
var ajax_url = '<?php echo admin_url('admin-ajax.php', is_ssl() ? 'https' : 'http'); ?>'; jQuery.get( ajax_url, YOUR_PARAMS_HERE, function(response) { // YOUR // CODE // GOES // HERE // ... and then, let's update the views count of this post / page jQuery.post( ajax_url, { action: 'update_views_ajax', token: '<?php echo wp_create_nonce('wpp-token') ?>', wpp_id: POST_ID_GOES_HERE }, function(response){ } ); } );
Of course, you’ll need to adapt this code to your needs. Pay attention to
YOUR_PARAMS_HERE
andPOST_ID_GOES_HERE
since you’ll need to change those as well.Have fun!
Viewing 12 replies - 1 through 12 (of 12 total)
Viewing 12 replies - 1 through 12 (of 12 total)
- The topic ‘How To: Updating the views count via AJAX’ is closed to new replies.