Hello!
I’m looking to update a custom field with a front end button.
I have a CPT of “shoe” and CF for that of “availability”.
I want to have a front end button of each “shoe” so that when a user clicks it, the “availability” CF increases by 1.
How could I accomplish this?
Thanks!

Hi @kkyang
Hmm… One possible way you can accomplish this is through the following:
First, create the button/link which the user clicks so as to increment availability CF. The code should be something like this (paste it into your template file)
$nonce = wp_create_nonce("increment_nonce");
$link = admin_url('admin-ajax.php?action=increment&post_id='.$post->ID.'&nonce='.$nonce);
echo '<a data-nonce="' . $nonce . '" href="' . $link . '">Increment Availability</a>';
When the user clicks the above, process increment the value using this code. Paste it in your functions.php file
add_action("wp_ajax_increment", "increment_cf");
add_action("wp_ajax_nopriv_increment", "increment_cf");
function increment_cf() {
if ( !wp_verify_nonce( $_REQUEST['nonce'], "increment_nonce")) {
exit("No naughty business please");
}
$post_id = $_REQUEST["post_id"];
$value = get_field('availability', $post_id);
$value++;
update_field('availability', $value, $post_id);
header("Location: ".$_SERVER["HTTP_REFERER"]);
die();
}
Hope this helps 🙂
For a better user experience, you can add JS to the mix and make an AJAX request instead. Check out this tutorial on AJAX