Skip Navigation

[Resolved] Increment count when link is clicked

This support ticket is created 5 years, 5 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 2 replies, has 2 voices.

Last updated by SatinS5964 5 years, 5 months ago.

Assisted by: Waqar.

Author
Posts
#1151459

I have a CPT with several fields along with a URL link and a counter field.

I have displayed this CPT with a View. In the view I also display the URL link field as well along with the other CPT fields.

I want to increment the counter field in the CPT whenever the URL link is clicked.

How do I increment that count field? Is there any custom shortcode or action or API hook I can call when the link is clicked?

Thanks,
Satin

#1151742

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi there,

Thank you for contacting us and I'll be happy to assist.

To update the count of a custom field, when a link is clicked, you'll need to use WordPress' AJAX calls.
( ref: https://codex.wordpress.org/AJAX_in_Plugins )

Although 1-1 assistance around custom programming is beyond the scope of support that we provide, we do our best to guide in the right direction whenever possible.

The code that you'll need for this can be divided into two parts:

1. You'll need to add the code to load some custom scripts and AJAX action, in your active theme's "functions.php" file.

For example:


// load and localize custom scripts
add_action( 'wp_enqueue_scripts', 'ajax_counter_link_enqueue_scripts' );
function ajax_counter_link_enqueue_scripts() {

	wp_enqueue_script( 'counter-link', get_theme_file_uri( '/js/link-counter.js' ), array('jquery'), '1.0', true );
	
	wp_localize_script( 'counter-link', 'counterlink', array(
		'ajax_url' => admin_url( 'admin-ajax.php' ),
		'ajax_nonce' => wp_create_nonce( 'link_click_counter_' . admin_url( 'admin-ajax.php' ) ),
	));

}

// action to execute AJAX call
add_action( 'wp_ajax_nopriv_click_counter_process', 'click_counter_process' );
add_action( 'wp_ajax_click_counter_process', 'click_counter_process' );

function click_counter_process() {

	// get current value of counter custom field
	$current_count = get_post_meta( $_POST['post_id'], 'wpcf-link-counter', true );

	// increment the counter value by 1
	$current_count++;

	if ( defined( 'DOING_AJAX' ) && DOING_AJAX && wp_verify_nonce( $_POST['nonce'], 'link_click_counter_' . admin_url( 'admin-ajax.php' ) ) ) {

		update_post_meta( $_POST['post_id'], 'wpcf-link-counter', $current_count );

		echo "pass";

		die();

	} else {

		die( 'Security check failed' );

	}
}


Notes: The above code block assumes that the custom field slug for the counter custom field that you're using is "link-counter". But you should update all instances of "wpcf-link-counter" to match your custom field's slug.

2. Next, you can create a new file "link-counter.js" in a folder named "js" in your active theme's folder and include the following code in it, to perform the AJAX requests on click event:


jQuery( document ).on( 'click', '.click-counter-link', function(event) {
	event.preventDefault();
	var post_id = jQuery(this).data('id');
	var post_URL = jQuery(this).attr('href');

	jQuery.ajax({
		url : counterlink.ajax_url,
		type : 'post',
		data : {
			action : 'click_counter_process',
			nonce : counterlink.ajax_nonce,
			post_id : post_id
		},
		success : function( response ) {

			if (response == "pass") {
				window.location.href = post_URL;
			}
			else
			{
				alert(response);
			}
			
		}
	});
})


After both these steps have been completed, you can add the actual links for each of your custom posts in the view like this:


<a class="click-counter-link" href="[wpv-post-url]" data-id="[wpv-post-id]">Click</a>

As a result, when visitors will click this link for a post through this view, they'll be redirected to that post and counter for the custom field value will be incremented by 1.

For a more personalize assistance around custom code, you can also consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/

I hope this helps.

regards,
Waqar

#1152078

My issue is resolved now. Thank you!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.