Skip Navigation

[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.

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)

Ce sujet contient 6 réponses, a 2 voix.

Dernière mise à jour par PaulS4783 Il y a 3 années et 10 mois.

Assisté par: Minesh.

Auteur
Publications
#1929029

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

thanks.

#1929107

Minesh
Supporter

Les langues: Anglais (English )

Fuseau horaire: Asia/Kolkata (GMT+05:30)

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

#1929195

For now, when the post is saved from the admin screen.

#1929281

Minesh
Supporter

Les langues: Anglais (English )

Fuseau horaire: Asia/Kolkata (GMT+05:30)

You can use the standard WordPress hook: save_post

Please check the example given with the following doc and adjust the save_post hook code accordingly:
=> https://toolset.com/documentation/customizing-sites-using-php/updating-types-fields-using-php/

#1929335

OK. That seems fairly clear.

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?

#1929341

Minesh
Supporter

Les langues: Anglais (English )

Fuseau horaire: Asia/Kolkata (GMT+05:30)

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.

#1930891

You can close this ticket.
I figured out the rest by myself.

cheers.