Is it possible to count how many times a certain link in post has been clicked?
(for example purpose, let's say that the certain link has an ID named 'bla')
<a id="bla" href="#">download</a>
I got a feeling it should be possible by using custom-fields/post-meta
(to keep the count), just like the ever-so-popular
'visitor count' trick. Unfortunately, I'm rather clueless about PHP
s.
One possible way is to redirect all through a common PHP gateway and from there, redirect to the original page you wanted to redirect using
Header('Location: yourpage.html');
In the gateway PHP page count the number by incrementing a saved value by 1.It could be done with ajax call that updates post meta field before the link is followed. Example below registers ajax action for users that are not logged in, and increases
link_click_counter
custom field by 1 on each click. Link must have id attributecountable_link
. This is a basic example that works for only one link in post. To use it as a plugin create file likewp-content/plugins/click-counter /click-counter.php
and copy-paste example code, or put the code infunctions.php
inside theme folder. First time the link is clicked, new custom fieldlink_click_counter
will be created for that post, and there you can track how many clicks link has.HTML:
PHP: