[Résolu] How to update the value of a custom field when the post is saved via a function
This support ticket is created Il y a 3 années et 10 mois. There's a good chance that you are reading advice that it now obsolete.
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.
Aucun de nos assistants n'est disponible aujourd'hui sur le forum Jeu d'outils. Veuillez créer un ticket, et nous nous le traiterons dès notre prochaine connexion. Merci de votre compréhension.
I have a custom post type "Vehicles".
There is a custom field for "Purchase Price"
and a custom field for "Insurance"
and a custom field for "Total Price"
Rather than have the user manually calculate and input the value for Total Price,
I would like to write some custom PHP code which takes the current value of "Purchase Price" + "Insurance", adds the two together and saves that into the custom field "Total Price".
To get me started on this, can you advise on:
a) a filter or action hook which gets triggered by the custom post save?
b) how to insert that value into the custom field "Total Price"?
Hello. Thank you for contacting the Toolset support.
I would like to know what you want to perform the automatic calculation;
- When you save the post from admin
- When you create new post using Toolset form
So in my example above, there is an input field for "Total Price" on the Dashboard input screen.
I'd like the system to ignore what the user inputs into that field (and populate only via a custom function).
Ideally I would like to hide it completely.
Is that possible?
If not, what would happen if the user enters a value into that field then saves the post?
Will the system accept the value they enter or will it ignore the entered amount and calculate the value as per the custom code?
Both input and calculation based on custom field value is possible.
You can not hide the input field in the backend as the value for the total price we will require to save. Maybe we can make it readonly but for that we will require to add custom JS or CSS to make that field readonly.
If you know how to make calculation then you should go ahead and use the save_post hook as per the example shared and adjust the code for save_post hook.
If you need any help there, please let me know but then I will require admin access details and please clarify based on what fields you want to made calculation with example values for each field and expected total price that will help me to deliver you the solution you want 🙂
*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.
I have set the next reply to private which means only you and I have access to it.