admin管理员组

文章数量:1289866

When the user clicks class count the counter should work:

<div class="pop-btn" class="counter"><a href="">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

When the user clicks class count the counter should work:

<div class="pop-btn" class="counter"><a href="https://www.example">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

Share Improve this question edited Jun 13, 2019 at 5:04 Vivek asked Jun 12, 2019 at 5:14 VivekVivek 439 bronze badges 6
  • 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 Commented Jun 12, 2019 at 5:59
  • @SallyCJ Can you help me with coding. how to do it via ajax in WordPress – Vivek Commented Jun 12, 2019 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 Commented Jun 12, 2019 at 8:30
  • @SallyCJ Please see the updated question. I would like to save the code on any file – Vivek Commented Jun 12, 2019 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 Commented Jun 12, 2019 at 15:39
 |  Show 1 more comment

1 Answer 1

Reset to default 1

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.

本文标签: phpclass click counter save number