Home Forums Feature Requests PHP Ajax Click Counter With ACF


PHP Ajax Click Counter With ACF

  • hello there in this point where im stuck I know its possible but because of my experience I don’t know how im gonna handle it 🙂 so im adding this future request Ibelive the idea some how important in one point – I mean how many times people click dynamically created specific custom field based links
    right now im using I generate every link with that web site so I can track the button activities.. ( I need only click but bitly gives me more than I bargain )
    lets get start it
    Source : Documentation and demos
    what I like to do but I cant do 🙂
    A) I need dynamically create a name for the .TXT file.
    B) create and save that file to the X location. because 2)
    2) if you checked the demo and examples script works ID based so manually you have to add that ID to the your link also in the counter.txt file
    like Data-id||times_clicked
    So I need automate this script to work for me but issue is “EXPERINCE” I don’t have 🙂
    just in case let me copy paste required 3 files here if > one day > some how > some one > with some experience fixed that issue > me my self and rest of the internet can use this useful information – or future with ACF


    html, body { margin:0; padding:0; font:16px/1.75 Verdana, Arial, Helvetica, sans-serif }
    .page-content { padding:1em; max-width:64em; margin:auto }
    .click-count { color:green; font-weight:bold }
    <div class="page-content">
    // my custom field For ID $uniqueid = get_field('fordigitid');
    // my custom field For Link 1 $linkname_1 = get_field('link1'); 
    // my custom field For Link 2 $linkname_2 = get_field('link2'); 
    // my custom field For Link 3 $linkname_3 = get_field('link3'); 
    $clickcount = explode("\n", file_get_contents('counter.txt')); // this .txt name must be dynamic 
    foreach($clickcount as $line){
    	$tmp = explode('||', $line);
    	$count[trim($tmp[0])] = trim($tmp[1]);
    <button class="click-trigger" data-click-id="click-001">Click Me</button> <!-- data-click-id have to be $uniqueid+$linkname_1 -->
    Clicked <span id="click-001" class="click-count"><?php echo $count['click-001'];?></span> times.  <!-- Same Here - $uniqueid+$linkname_1 -->
    <button class="click-trigger" data-click-id="click-002">Click Me</button>  <!-- data-click-id have to be $uniqueid+$linkname_2 -->
    Clicked <span id="click-002" class="click-count"><?php echo $count['click-002'];?></span> times. <!-- Same Here - $uniqueid+$linkname_2 -->
    <button class="click-trigger" data-click-id="click-003">Click Me</button>   <!-- data-click-id have to be $uniqueid+$linkname_3 -->
    Clicked <span id="click-003" class="click-count"><?php echo $count['click-003'];?></span> times. <!-- Same Here - $uniqueid+$linkname_2 -->
    </div><!-- closing ".page-content" -->
    var clicks = document.querySelectorAll('.click-trigger'); // IE8
    for(var i = 0; i < clicks.length; i++){
    	clicks[i].onclick = function(){
    		var id = this.getAttribute('data-click-id');
    		var post = 'id='+id; // post string
    		var req = new XMLHttpRequest();'POST', 'counter.php', true); 
    		req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    		req.onreadystatechange = function(){
    			if (req.readyState != 4 || req.status != 200) return; 
    			document.getElementById(id).innerHTML = req.responseText;


    /* so this is the point !!! I have to automate that */
    /* Notes - add items manually to 'counter.txt' 
               one item per line, in this format;
               e.g. click-005||0
               e.g. my-id||0
               e.g. download-01||0
    $file = 'counter.txt'; // path to text file that stores counts - Also here this TXT file name must be dynamic so I cant post every single post click stats individually 
    $fh = fopen($file, 'r+');
    $id = $_REQUEST['id']; // posted from page
    $lines = '';
    	$line = explode('||', fgets($fh));
    	$item = trim($line[0]);
    	$num = trim($line[1]);
    		if($item == $id){
    			$num++; // increment count by 1
    			echo $num;
    		$lines .= "$item||$num\r\n";
    file_put_contents($file, $lines);



    How To show in the post or page

    <span id="click-001"><?php echo $count['click-001'];?></span>

    I think we all set now thank you for reading and helping me to solve this puzzle 🙂

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.