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 | Show 1 more comment1 Answer
Reset to default 1Made 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
版权声明:本文标题:php - class click counter save number 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741485708a2381392.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
data : { 'increase' : '1' }
should include the AJAX action -data : { 'increase' : '1', action: 'more_post_ajax' }
. – Sally CJ Commented Jun 12, 2019 at 15:39