0

I need to count how many times a user clicked a specific button, for example:

<a id="download" href="#">Download</a>

What´s best practice here? I think about to create a completely new database table and increment the value each time a user clicks on the button.

function countClicks() {
 global $wpdb;
 $charset_collate = $wpdb->get_charset_collate();
 require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );

 //* Create the teams table
 $table_name = $wpdb->prefix . 'count_clicks';
 $sql = "CREATE TABLE $table_name (
 click_id INTEGER NOT NULL AUTO_INCREMENT,
 click_number INTEGER NOT NULL,
 PRIMARY KEY (click_id)
 ) $charset_collate;";
 dbDelta( $sql );
}

Do you have better solutions for this instead of creating a fresh new table? Btw: It´s a WordPress website.

Vueer
  • 1,432
  • 3
  • 21
  • 57

2 Answers2

1

To increment a counter and have it without creating a new table. You should use the options API. This is core update safe.

First, include the following in your theme scripts which I'm assuming you have a local theme script or plugin js files. You must localize your script for using ajax in the "WP Way"

jQuery(document).ready(function($){
    $('a#download').click(function(e){
        e.preventDefault();
        $.ajax({
            url: myajax.ajaxurl,
            data: {
                action: 'increment_counter',
            },
            type: 'POST',
        })
        .done(function(){
            // go to the link they clicked
            window.location = $(this).attr('href');
        })
        .fail(function(xhr){
            console.log(xhr);
        })
    });
});

Then add the following to your functions.php or in your plugin.

add_action('wp_ajax_increment_counter', 'my_increment_counter');   
add_action('wp_ajax_nopriv_increment_counter', 'my_increment_counter');

function my_increment_counter(){
    // Name of the option
    $option_name = 'my_click_counter';
    // Check if the option is set already
    if ( get_option( $option_name ) !== false ) {
        $new_value = intval(get_option($option_name)) + 1; 
        // The option already exists, so update it.
        update_option( $option_name, $new_value );
    } else {
        // The option hasn't been created yet, so add it with $autoload set to 'no'.
        $deprecated = null;
        $autoload = 'no';
        add_option( $option_name, 1 , $deprecated, $autoload );
    }
}

So whenever you need to get the value without incrementing it, just use

get_option('my_click_counter')

Howard E
  • 5,454
  • 3
  • 15
  • 24
0

I would use the Advanced Custom Fields plugin and create a custom field for each downloadable file - then use JavaScript to capture the click which via AJAX sends a call to a function that increments the count.

Tokant
  • 312
  • 1
  • 8
  • Can I do this also without any plugin? :-) – Vueer May 05 '20 at 04:43
  • Yes, without a plugin you can code the functionality that adds a custom field to each downloadable file and updates the count via JavaScript and AJAX. – Tokant May 05 '20 at 08:34