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.
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:
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.
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/