Skip Navigation

[Resolved] How to increment a number field by 1 on form submit

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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

This topic contains 3 replies, has 1 voice.

Last updated by Minesh 1 week, 2 days ago.

Assisted by: Minesh.

Author
Posts
#2789715

Hi,

I have a front-end Edit form and I want to have a number field automatically incremented by 1, from whatever it currently is, when the form is submitted. And this would be on a page with multiple Post Edit forms so I need it to avoid updating all forms, just the one being submitted.

Thanks,

Tim

#2789840

Christopher Amirian
Supporter

Languages: English (English )

Hi Tim,

Welcome to Toolset support.

To achieve this functionality in Toolset Forms, you can use custom PHP code to automatically increment a number field on the specific post being edited. Toolset provides hooks that allow you to add custom logic to form submissions. Here's how you can do it:

Add the code below to the custom PHP feature of Toolset:

https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/

add_action('cred_save_data', 'increment_number_field', 10, 2);
function increment_number_field($post_id, $form_data) {
    // Specify the ID(s) of your Edit forms where this should apply
    $target_form_ids = array(123, 456); // Replace with your actual form IDs

    // Check if the submitted form is one of the target forms
    if (in_array($form_data['id'], $target_form_ids)) {
        // Get the current value of the custom field
        $current_value = get_post_meta($post_id, 'wpcf-your-number-field', true); // Replace 'your-number-field' with your actual field slug
        
        // Ensure it's a number and increment it
        $new_value = (int)$current_value + 1;

        // Update the custom field with the incremented value
        update_post_meta($post_id, 'wpcf-your-number-field', $new_value);
    }
}

Replace 123, 456 in $target_form_ids with the ID(s) of your Edit forms.

Replace your-number-field with the slug of your number field in the post meta.

Ensure the field is a single-line number field in Toolset so that the data is properly validated as an integer.
The prefix wpcf- is automatically added to Toolset custom fields when stored in the database. This is why it is included in the get_post_meta and update_post_meta functions.

For more information:

https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

Please Note: This is considered a custom development and it is outside of our support scope. We provided the starting point for the customization but fine tuning and additional development can not be done by us.
If you need additional help you are welcome to hire a developer:
https://toolset.com/contractors/

#2790427

This is great, thanks so much!

Tim

#2790574

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Glad to know that solution shred by Christofer help you to resolve your issue. Yo're welcome to mark resolve this ticket.