0

i'm using this function for click count of downloads in wordpress.
Function.php

function setdownViews($postID) {
$dcount_key = 'post_download_count';
$dcount = get_post_meta($postID, $dcount_key, true);
if($dcount==''){
    $dcount = 0;
    delete_post_meta($postID, $dcount_key);
    add_post_meta($postID, $dcount_key, '0');
}else{
    $dcount++;
    update_post_meta($postID, $dcount_key, $dcount);
}
}

and this jequery code to call this function after click on download link.

$(document).on('click', "#click-count", function () {
    <?php setdownViews(get_the_ID());?>
});  

but not working and meta value of post_download_count not updating. i know i should use ajax and jquery to call this function but don't know how.
best regards.

3
  • what error are you getting? Commented Jan 22, 2015 at 6:44
  • does it come to the click function? Commented Jan 22, 2015 at 6:49
  • I'm not getting any error but noting happen also and function not work. Commented Jan 22, 2015 at 6:55

2 Answers 2

0

You can't call php function in javascript like that. What php does is generate html (include your js), then js will be executed in browser.

You must move your code for increasing downloads number (code inside setdownViews function) to a separate page. When you open that page in the browser, if the downloads number is increased, then it's working. After that, use js (jquery in your case) to call that page.

If the new page name set-downloads-view.php, if could call something like set-downloads-view.php?id=1. Then get the id in php by $_GET['id']

Here is the code:

set-downloads-view.php:

require( dirname( __FILE__ ) . '/wp-blog-header.php' );
$postID = $_GET['id'];
$dcount_key = 'post_download_count';
$dcount = get_post_meta($postID, $dcount_key, true);
if($dcount==''){
    $dcount = 0;
    delete_post_meta($postID, $dcount_key);
    add_post_meta($postID, $dcount_key, '0');
}else{
    $dcount++;
    update_post_meta($postID, $dcount_key, $dcount);
}

javascript

$(document).on('click', "#click-count", function () {
    $.ajax ({
        url: "set-downloads-view.php?id=<?php get_the_ID();?>"),
        data: null,
        success: function( result ) {
            // it\'s done, show something to the user
        }
    });
});  

I'm sorry in advance that I haven't used wordpress in very long time, but you get the idea. It's not the best solution, but it should work.

Sign up to request clarification or add additional context in comments.

8 Comments

thank you HoangND. i'm not getting error,but value of meta key not updating
if you open this page in browser, will it increase the meta: set-downloads-view.php?id=your-id
i open the url in browser:localhost/2/wp-content/themes/mocup/… . and this the error : ( ! ) Warning: require(E:\wamp\www\2\wp-content\themes\mocup/wp-blog-header.php): failed to open stream: No such file or directory in E:\wamp\www\2\wp-content\themes\mocup\set-downloads-view.php on line 2
For the the code I gave you, you need to put that file in the same folder with index.php
you mean in theme directory or root of wordpress files?
|
0

we can't call php function like this, try to use ajax. try this Eg:
index.php page

$(document).ready(function(){
$(document).on('click', "#click-count", function () {
    var val = "Hi";
    $.ajax ({
        url: "ajax.php",
        data: { val : val },
        success: function( result ) {
            alert("Hi, testing");
            alert( result );
        }
    });
});
});

add new file name as ajax.php page and put the below code in it

<?php
echo ( $_GET['val'] );?>

1 Comment

I edited the code. add a new file name ajax.php in same folder. If you get the alert Hi. then this will work.

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.