Skip Navigation

[Resolved] Saving value from generic form field to a custom field in CPT.

This thread is resolved. Here is a description of the problem and solution.

Problem:
How can we save data from a generic field to any post field or post title/body, when submitting a Toolset Forms?

Solution:
There is a full explanation here https://toolset.com/forums/topic/saving-value-from-standard-generic-form-field-to-a-custom-field-in-cpt/#post-1489201, in short, you'd use cred_save_data() to get the values from the $_POST['form-field-slug'] and update_post_meta() that value to the Post meta you want to udpate, or of course, update anything else with the WordPress API in the post.

This support ticket is created 4 years, 9 months ago. 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.

Sun Mon Tue Wed Thu Fri Sat
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 2 replies, has 2 voices.

Last updated by geophray 4 years, 9 months ago.

Assisted by: Beda.

Author
Posts
#1489115

Tell us what you are trying to do?
I'm wondering if it is possible to save the value from a custom form field into a custom field of the post being created by that form–one that is not inserted to the form using the cred fields shortcodes, but rather is coded directly to the form like this:

    <span class="star-rating star-5">
        <input type="radio" name="wpcf-review-rating" value="1"><i></i>
        <input type="radio" name="wpcf-review-rating" value="2"><i></i>
        <input type="radio" name="wpcf-review-rating" value="3"><i></i>
        <input type="radio" name="wpcf-review-rating" value="4"><i></i>
        <input type="radio" name="wpcf-review-rating" value="5"><i></i>
    </span>

I'm trying to create an input that is styled like the one in this jsfiddle: hidden link

I want it to save the value to the wpcf-review-rating that is a custom number field for the "review" CPT I have created.

Is there any documentation that you are following?
I have been able to get the value to save the to form field using a generic cred input field as outlined here: https://toolset.com/forums/topic/save-cred-generic-field-value-to-custom-field/

However, because the HTML structure is different I'm having trouble getting the styling to work as shown in the jsfiddle, and if it's possible to hard code this field in rather than have it generated from CRED I would prefer that.

What is the link to your site?
The link to my cred form is hidden link
The site is in development and has privacy mode enabled, so you will need the following access info to view it:
u/n rodlimb
p/w communicating-love

#1489201

You can either add Inputs with manual HTML as you did or you can use Generic Fields of Forms.
In both cases, no mechanism will save them to the database, and those inputs are just that: inputs, that will not be saving their values on submit of the form

When using generic Fields as provided by Toolset you could set a "persist:1" argument (deprecated but working), this would save the fields to that post in the database, but, it would not be editable in the backend of the post as a Toolset Field is.
This is what you found here https://toolset.com/forums/topic/save-cred-generic-field-value-to-custom-field/

In general, it is rather suggested to - if you really require such inputs and cannot do it with a Toolset field - to use a Custom code to read the data from the $_POST of the form, and update with this your target data in the database, hooking your code to a cred_save_data() hook

cred_save_data() is like a "hook" in the life-span of a Form. It is triggered exactly when you submit that form
This means you can wrap any custom code with it, and when the form is submitted that code will fire.

That code is what will read your Input data from the $_POST and using update_post_meta() WordPress API function, you could then update other fields with this.
An example can be found on our doc here https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data and in my Pastebin here https://pastebin.com/2BTbVbcs

To get a value from an input field on a form you can usually do $value = $POST['name-of-that-field']
Using this $value later in update_post_meta(), wrapped inside a cred_save_data(), allows then to update the database with the value coming from $value = $POST['name-of-that-field']

I can help with this limitedly only, I can assist and show examples, but not create the full code.
For full custom code assistance, you would have to refer to contractors, for example, https://toolset.com/contractors/

Thanks!

#1489373

Thank you so much Beda! With your giant push in the right direction, I was able to create the function needed to store the value from my custom form field. Greatly appreciated!