Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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 <code>link_click_counter</code> custom field by 1 on each click. Link must have id attribute <code>countable_link</code>. This is a basic example that works for only one link in post. To use it as a plugin create file like <code>wp-content/plugins/click-counter /click-counter.php</code> and copy-paste example code, or put the code in <code>functions.php</code> inside theme folder. First time the link is clicked, new custom field <code>link_click_counter</code> will be created for that post, and there you can track how many clicks link has.</p> <p>HTML:</p> <pre><code>&lt;a id="countable_link" href="#"&gt;download&lt;/a&gt; </code></pre> <p>PHP:</p> <pre><code>&lt;?php /* Plugin Name: Link Clicks Counter */ if ( is_admin() ) add_action( 'wp_ajax_nopriv_link_click_counter', 'link_click_counter' ); function link_click_counter() { if ( isset( $_POST['nonce'] ) &amp;&amp; isset( $_POST['post_id'] ) &amp;&amp; wp_verify_nonce( $_POST['nonce'], 'link_click_counter_' . $_POST['post_id'] ) ) { $count = get_post_meta( $_POST['post_id'], 'link_click_counter', true ); update_post_meta( $_POST['post_id'], 'link_click_counter', ( $count === '' ? 1 : $count + 1 ) ); } exit(); } add_action( 'wp_head', 'link_click_head' ); function link_click_head() { global $post; if( isset( $post-&gt;ID ) ) { ?&gt; &lt;script type="text/javascript" &gt; jQuery(function ($) { var ajax_options = { action: 'link_click_counter', nonce: '&lt;?php echo wp_create_nonce( 'link_click_counter_' . $post-&gt;ID ); ?&gt;', ajaxurl: '&lt;?php echo admin_url( 'admin-ajax.php' ); ?&gt;', post_id: '&lt;?php echo $post-&gt;ID; ?&gt;' }; $( '#countable_link' ).on( 'click', function() { var self = $( this ); $.post( ajax_options.ajaxurl, ajax_options, function() { window.location.href = self.attr( "href" ); }); return false; }); }); &lt;/script&gt; &lt;?php } } ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload