Skip Navigation

[Gelöst] Split: – combine two custom field value and save automatically with form submit

Dieser Thread wurde gelöst. Hier ist eine Beschreibung des Problems und der Lösung.

Problem:
Split: - combine two custom field value and save automatically with form submit

Solution:
You should try to use the Toolset form's hook "cred_save_data" to combine the custom fields values and update it as post title.

You can find the proposed solution in this case with the following reply:
=> https://toolset.com/forums/topic/split-combine-two-custom-field-value-and-save-automatically-with-form-submit/#post-1365469

Relevant Documentation:
=> https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

This support ticket is created vor 5 Jahren, 1 Monat. 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.

Heute stehen keine Supporter zur Arbeit im Werkzeugsatz-Forum zur Verfügung. Sie können gern Tickets erstellen, die wir bearbeiten werden, sobald wir online sind. Vielen Dank für Ihr Verständnis.

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)

Dieses Thema enthält 4 Antworten, hat 2 Stimmen.

Zuletzt aktualisiert von geoffD vor 5 Jahren, 1 Monat.

Assistiert von: Minesh.

Author
Artikel
#1365391

Hi Minesh...

Ok I have been doing some work on this and have found some solutions..so lets start again from the beginning!

I have 3 problems:
1. When I'm in Access Control to set permissions for my various user roles for the various forms it will not save my changes.

2. I have a form with 3 fields - (first-name, surname and full-name) - When submitting the form I need for the full-name field to be automatically filled and saved with the first name and surname merged with a space between
e.g.
first-name: George
surname: Dawson
full-name: George Dawson

3. I have a CPT 'PERSON' that the user creates but can only create one of so - $person[0] I also have a CPT 'PARTNER PAGE' that again the user can only create one of so $partner_page[0]... When I go to the create 'PARTNER PAGE' or the edit 'PARTNER PAGE' I want the fields:
first name
surname
Automatically filled with the first name and surname fields from the CPT PERSON ($person[0])
I need to create some hook that retrieves the PERSON information whenever the create PARTNER PAGE or edit PARTNER page is called

I shall look forward to your response

Best regards
Geoff

#1365395

Minesh
Supporter

Sprachen: Englisch (English )

Zeitzone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

You can use the Toolset Form's hook cred_save_data in order to combine two form field values.

For example - Add the following code to your current theme's functions.php file or "Custom Code" section offered by Toolset:
=> https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/

//Create a dynamic post title by the CRED form.
add_action('cred_save_data','func_custom_post_title',10,2);
function func_custom_post_title($post_id,$form_data) {

    if ($form_data['id']==9999) {

        $name = get_post_meta($post_id, 'wpcf-first-name', true);
        $surname = get_post_meta($post_id, 'wpcf-surname', true);

        $fullname = $name. ' ' .$surname ;

         update_post_meta($post_id, 'wpcf-full-name', $fullname);
       
    }
}

Where:
- Replace 9999 with your original form ID

More info:
=> https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

#1365457

Hi Minesh

The field that I actually need filling is not 'full-name' it is the post title..so I added the following code to the functions file but it is not working and auto creating a post title:

[code]add_action('cred_save_data','func_custom_post_title',10,2);
function func_custom_post_title($post_id,$form_data) {

if ($form_data['id']==1678) {

$name = get_post_meta($post_id, 'wpcf-first-name', true);
$surname = get_post_meta($post_id, 'wpcf-surname', true);

$fullname = $name. ' ' .$surname ;

update_post_meta($post_id, $post_title, $fullname);

}
}[/code]

#1365469

Minesh
Supporter

Sprachen: Englisch (English )

Zeitzone: Asia/Kolkata (GMT+05:30)

If you want to update the post title you need to use the following code;

add_action('cred_save_data','func_custom_post_title',10,2);
function func_custom_post_title($post_id,$form_data) {

if ($form_data['id']==1678) {

$name = get_post_meta($post_id, 'wpcf-first-name', true);
$surname = get_post_meta($post_id, 'wpcf-surname', true);

$title = $name. ' ' .$surname ;

$args = array('ID' => $post_id, 'post_title' => $title);
wp_update_post($args);

}
}
#1365519

That's amazing Minesh. Thank you!