1

When the user clicks class count the counter should work:

<div class="pop-btn" class="counter"><a href="https://www.example.com">Know More</a></div>
<div class="count"></div>

The problem is that I want the number to be saved, so for example:

user #1 clicks on the div with class "counter" 10 times

user #2 opens the site and the count is at 10, he clicks and now the counter starts 11 and so on with new users.

I'm trying to do this to keep track of how many times a file has been downloaded.

Any idea how can I do that?

Here is the code I have:

functions.php

function js_enqueue_scripts() {
wp_enqueue_script ("my-ajax-handle", get_stylesheet_directory_uri() . "/js/counter.js", array('jquery')); 
wp_localize_script('my-ajax-handle', 'the_ajax_script', array('ajaxurl' =>admin_url('admin-ajax.php')));
} 
add_action("wp_enqueue_scripts", "js_enqueue_scripts");


function set_button_count(){

  $counterFile = 'counter.txt' ;

// jQuery ajax request is sent here
if ( isset($_POST['increase']) )
{
    if ( ( $counter = @file_get_contents($counterFile) ) === false ) die('Error : file counter does not exist') ;
    file_put_contents($counterFile,++$counter) ;
    echo $counter ;
    return false ;
}

if ( ! $counter = @file_get_contents($counterFile) )
{
    if ( ! $myfile = fopen($counterFile,'w') )
        die('Unable to create counter file !!') ;
    chmod($counterFile,0644);
    file_put_contents($counterFile,0) ;
}


}
add_action('wp_ajax_nopriv_set_button_count', 'set_button_count');
add_action('wp_ajax_set_button_count', 'set_button_count');

Counter.js

jQuery(document).on('click','.counter',function(){
jQuery('.count').html('Loading...');
var ajax = jQuery.ajax({
method : 'post',
url : the_ajax_script.ajaxurl, // Link to this page
data : { 'action':'set_button_count','increase' : '1' }
});
ajax.done(function(data){
jQuery('.count').html(data) ;
});
ajax.fail(function(data){
alert('ajax fail : url of ajax request is not reachable');
});
});

Console Error: Error 400 Post /wp-admin/admin-ajax.php

  • You can use custom post type and use the post meta table to store the download counts, and use AJAX to track the link clicks. Or use an existing solution such as Download Monitor or WordPress Download Manager. – Sally CJ Jun 12 at 5:59
  • @SallyCJ Can you help me with coding. how to do it via ajax in WordPress – Vivek Jun 12 at 7:29
  • Actually, by "file", did you mean a WordPress attachment (in the media library) or just any files on your site? Do you already have the PHP code which saves the counts value? If so, can you post the code? – Sally CJ Jun 12 at 8:30
  • @SallyCJ Please see the updated question. I would like to save the code on any file – Vivek Jun 12 at 9:45
  • I haven't tested your code, but the data : { 'increase' : '1' } should include the AJAX action - data : { 'increase' : '1', action: 'more_post_ajax' }. – Sally CJ Jun 12 at 15:39
0

Made ajax call as a function to avoid the Console Error 400 Post. It opens the channel and I was able to get the parameters passed.

counter.js

 jQuery(document).on('click','.counter',function(e){
 testAjax(the_ajax_script.ajaxurl);
 });


function testAjax(link) {
    $.ajax({
    url: link,
    type: 'post',
    data: {
    'action': 'set_button_count', 
    'increase' : '1'
  },  
  success: function(data) {
     jQuery('.count').html(data);
  }
  });
}

In functions.php I changed the $counterFile variable value to the link of a text file, which I uploaded via the WordPress admin panel. I specified the link as the server directory path, to avoid the error["failed to open stream: HTTP wrapper does not support writeable connections"].

functions.php

function js_enqueue_scripts() {
wp_enqueue_script ("my-ajax-handle", get_stylesheet_directory_uri() . "/js/counter.js", array('jquery')); 
wp_localize_script('my-ajax-handle', 'the_ajax_script', array('ajaxurl' =>admin_url('admin-ajax.php')));
} 
add_action("wp_enqueue_scripts", "js_enqueue_scripts");


function set_button_count(){

  $counterFile = '/home/example/public_html/wp-content/uploads/2019/06/counter.txt';
  if ( isset($_POST['increase']) )
{
    if ( ( $counter = @file_get_contents($counterFile) ) === false ) die('Error : file counter does not exist') ;
    file_put_contents($counterFile,++$counter);
    echo file_get_contents($counterFile);
    wp_die();
}

if ( ! $counter = @file_get_contents($counterFile) )
{
    if ( ! $myfile = fopen($counterFile,'w') )
        die('Unable to create counter file !!') ;
    chmod($counterFile,0644);
    file_put_contents($counterFile,0) ;
}
wp_die();

}
add_action('wp_ajax_nopriv_set_button_count', 'set_button_count');
add_action('wp_ajax_set_button_count', 'set_button_count');

Hope it helps someone.

  • Ok, and don't forget to accept your answer. :) Cheers! (PS: I've removed my answer.) – Sally CJ Jun 16 at 10:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.