1

I have a simple PHP based counter Wordpress function that updates the user meta every time the page is loaded.

What I would like to do is only run this function when a particular button is clicked.

My button markup

<a class="button" id="interaction-count" href="#">Interaction</a>

My current PHP function (Which currently runs, and updates the user meta, on each page load)

function setCvDownloads($postID) {
    $count_key = 'download_count';
    $count = get_user_meta($postID, $count_key, true);
    if($count==''){
        $count = 0;
        delete_user_meta($postID, $count_key);
        add_user_meta($postID, $count_key, '0');
    }else{
        $count++;
        update_user_meta($postID, $count_key, $count);
    }
}

Is is possible to run this when the interaction-count button is clicked instead? I assume some AJAX would be involved? If someone couple point me in the right direction that would be great

5
  • Correct - it is possible to run it when the interaction-count button is clicked and also yes, ajax would probably be the best option. Commented Sep 1, 2016 at 16:34
  • From Where you will get the $myValue value... Commented Sep 1, 2016 at 16:36
  • And another note you have not used $myValue anywhere in your function. Commented Sep 1, 2016 at 16:37
  • @NareshKumar.P Yes sorry, it was an old value, I have edited my question Commented Sep 1, 2016 at 17:04
  • That means you can choose the ajax method and it will work fine.. Will post up the ajax function asap. Have a check of it.. Commented Sep 1, 2016 at 17:06

4 Answers 4

1

Usage of the Ajax is the best option that will proceed.

<a class="button" id="interaction-count" href="#" onclick="setCvDownloads(<?php echo $post->ID);">Interaction</a>

You need to pass the post ID in that function since you have used it over to the function.

Ajax:

Make sure your file-path is correct in the URL of the Ajax syntax or else t will not work.

function setCvDownloads(a)
{
    $.ajax({
      type: "POST",
      url: "/savedata.php",
      data: "&post_id="+a,
      success:function(html)
      {
         // here you can provide if you need success notifications or as per your wish.
      }
    });
}

savedata.php

<?php
include('../../wp-config.php'); // Ensure that your wp-config file is connected here correctly.
$post_ID = $_POST['post_id']; // Getting the posted POST ID over ajax here.
$count_key = 'download_count';
    $count = get_user_meta($post_ID, $count_key, true);
    if($count==''){
        $count = 0;
        delete_user_meta($post_ID, $count_key);
        add_user_meta($post_ID, $count_key, '0');
    }else{
        $count++;
        update_user_meta($post_ID, $count_key, $count);
    }
?>
Sign up to request clarification or add additional context in comments.

2 Comments

The setCvDownloads function that I currently have is a PHP function so I assume it wont work in JS?
@DeanElliott. I have added the Ajax code over to my answer by editing it. Check and let me know. :)
1

Using jQuery. This jQuery code should do it, but you need a PHP file to capture the request.

$("#interaction-count").on("click", function(){
    $.ajax({
      type: "POST",
      url: "/somephpfile.php",
      data: {},
      success: success,
      dataType: dataType
    });
});

Comments

0

You can do also synchronously, but the best way is surely usin ajax.

So the code, in simple js (without jquery), would be:

<input type="button" ... onclick="CallPhpFunction()" />
....
<script>
function CallPhpFunction() {
    var xhttp=new XMLHttpRequest();
    xhttp.open('POST', 'phpscript.php', true);
    xhttp.send('data=somedata&otherData=someotherdata');
}
</script>

In the example I've also show how you can send data to the php script passing it to the send method. That can be used in the php script using $_POST.

Comments

0

for doing this you need to add following code inside wordpress loop

<a class="button" id="interaction-count" href="#"
 onclick="increaseMyCount('<?php the_ID();?>')">Interaction</a>

<script type="text/javascript">
function increaseMyCount(numb_data)
{
jQuery.ajax({
method: "POST",
url: "<?php bloginfo('url');?>/wp-admin/admin-ajax.php",
data: { postid: numb_data, action: 'setcvdownloads' }
})
.done(function( msg ) {
alert( "Data Saved: " + msg );
});
}
</script>

and in functions.php of theme file you need to write following code

<?php
function setCvDownloads() {
    $count_key = 'download_count';
    $postID=$_POST['postid'];
    $count = get_user_meta($postID, $count_key, true);
    if($count==''){
    $count = 0;
    //delete_user_meta($postID, $count_key);
    add_user_meta($postID, $count_key, '0');
    }else{
        $count++;
        update_user_meta($postID, $count_key, $count);
    }
    echo "Saved";
    wp_die();
}
add_action( 'wp_ajax_setcvdownloads', 'setCvDownloads' );
add_action( 'wp_ajax_nopriv_setcvdownloads', 'setCvDownloads' );
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.