What sorcery is this?


#1

So… I love the whole, click on a link and the counter increments feature of this forum. Can the devs tell us how they did it? I’d love to add this to my WordPress site… :smile:


#2

This forum uses http://www.discourse.org/, so you can check it out there.


#3

Yup, we use discourse for the forum


#4

hmmm… Thanks! :smile:


#5

This can be done by attaching a listener to all links inside a post. For example in jquery:

$(".post a").on("click", function(){
//ajax call to a DB with the link identifier
});

Previously you must give to all links a identifier (maybe using data-) in order to know what link has been clicked. Or maybe you can save all links of your posts on a database, and when a link is clicked, you can go to your database and compare the href attribute to sum +1.


#6

You don’t need a data-attribute since you can catch the clicked link with this in JavaScript or get the URL in the event handler (shown below).
Only thing you should be careful about is not logging “internal links” (such as logout link)

$('.post').on('a', 'click', {url: url}, function(event) {
    var post_id;
    // get post id
    var query = "SELECT count FROM *.* WHERE POST_ID = " + post_id + " AND URL = " + event.data.url + ";";
    var result = // execute query
    query = "UPDATE .... " + result+1;

    // both queries could also be combined into 1
});

#7

little fix here:

 $('.post').on('click', 'a', {url: url}, function(event) {
 ...