Home Forums ACF PRO Updating a CPT CF from the front end.


Updating a CPT CF from the front end.

  • 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?


  • 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);
       update_field('availability', $value, $post_id);
       header("Location: ".$_SERVER["HTTP_REFERER"]);

    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

Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Updating a CPT CF from the front end.’ is closed to new replies.