Is there a hook that triggers when a field is changed, before saving?
My issue is that I would like to calculate a endtime from a starttime (time/date field). So when a user choose a date and time it will automatically do a calculation on a distance field and speed field and auto-fill the end time.
The calculation is simple but I cant figure out when it gets triggered.
Hi Bee,
Thanks for this, still trying to get my head around programming. I should probably have said that I am working in the backend of WP, would this filter work in the backend? And when is this filter triggered?
Yes it will work in the back-end. Please check the links. It has all the info.
Nop,
I am not getting it, I can’t get acf/pre_save_post to trigger in the backend at all and read in another post that it wont work on the backend, so confused.
If anyone has an idea how I can calculate a value from two field before I “save Post, ex. I add speed in one field and time in another then it should calculate ETA and autofill a second time field, ps I also have a distance field to do the caclulation.
Any ideas or pointers in the right direction would be appreciated,
Thanks
This should give you a nudge in the direction.
This code needs to placed in functions.php.
function acf_57448( $post_id ) {
$start_date = get_field( 'field_name', $post_id );
// do stuff to calculate ETA
update_field( 'eta_field_name', $post_id )
}
add_action( 'acf/save_post', 'acf_57448', 20 );
Hi Bee,
Thanks for your help but you misunderstand (or do I?), I need to fill the eta field out as soon as the ETD and speed is filled out (on field change) before I save the post. Like when you tab to another field it auto fills the other one, as in many computer software, excel is one exemple. hope it make sense!
Ah ok…. my code gets generated after saving.
What you want is some Javascript, which does something on.change (I think), but that is not my strong point so can’t help you there.
See this documentation https://www.advancedcustomfields.com/resources/adding-custom-javascript-fields/
and this topic that has an example of setting and end date based on a start date https://support.advancedcustomfields.com/forums/topic/autocomplete-end-date-when-start-date-is-selected-in-jquery-date-picker/
It is not exactly what you need, but with a little work it will get you there.
Hi John,
I am terrible sorry for taking such a time to come back and thank you for your answer, has been a busy time. I will look in to it one day as it is still something I want done one day.
Thanks again for your time,
Kru-x
The topic ‘Hook on field change’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.