Skip Navigation

[Resolved] Create CPT Title from CRED form (two fields)

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

Problem:
How to create custom or dynamic title with for post created using CRED form
Solution:
CRED API offers you hook "cred_save_data" to accoplish this goal. Also, custom fields values stored in postmeta table. You can get the custom field value using get_post_meta() function.

For example:
https://toolset.com/forums/topic/create-cpt-title-from-cred-form-two-fields/#post-380121

Relevant Documentation:
=> https://toolset.com/documentation/user-guides/cred-api/#csd

This support ticket is created 8 years 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
- 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)

This topic contains 10 replies, has 2 voices.

Last updated by jonS-2 8 years ago.

Assisted by: Minesh.

Author
Posts
#379461

I have been on this for hours and cant seem to get the syntax right. I've read the forums and it hasn't helped.
I have a custom post type called "LittleChamps"
I have a CRED form "ID=87"
This form creates data for this CPT. There are two custom fields within this CPT called "first-name" and "surname".
Can you please give me the correct syntax for saving the TITLE of the CPT with the combination of "first-name" and "surname".
Example:
"jones-tom"

Thankyou in advance.

#379470

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

CRED API offers you hooks to accomplish that:
"cred_save_data" hook:
https://toolset.com/documentation/user-guides/cred-api/#csd

Try to add following code to your current theme's functions.php file.

For example:

add_action('cred_save_data','custom_title', 10, 2);
function custom_title($post_id) {
// if a specific form
if ($form_data['id']==87)  {
    
        $fname= get_post_meta($post_id, 'wpcf-first-name', true);
        $sname= get_post_meta($post_id, 'wpcf-surname', true);
        $title = $fname."-". $sname;
        wp_update_post(array('ID' => $post_id, 'post_title' => $key));
    
}
}

I hope above solution will help you to resolve your issue.

#379676

Thanks for your prompt reply, but this doesn't seem to have worked. I still get "Auto Save" as the title of the post.

#379677

Correction "Auto Draft"

#379806

Minesh
Supporter

Languages: English (English )

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

Sorry.

Please use following code - there is a typo and I forget to change variable name:

add_action('cred_save_data','custom_title', 10, 2);
function custom_title($post_id) {
// if a specific form
if ($form_data['id']==87)  {
     
        $fname= get_post_meta($post_id, 'wpcf-first-name', true);
        $sname= get_post_meta($post_id, 'wpcf-surname', true);
        $title = $fname."-". $sname;
        wp_update_post(array('ID' => $post_id, 'post_title' => $title ));
     
}
}
#379866

Aaargh!
I have added in your Version 2.0, but the result is the same.
Any ideas?

#380065

Minesh
Supporter

Languages: English (English )

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

It's really strange.

Could you please share link of the page where you've added CRED form.

Let me debug on your install to fix this issue.

*** 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 would additionally need your permission to de- and re-activate Plugins and the Theme, and to change configurations on the site. This is also a reason the backup is really important. If you agree to this, please use the form fields I have enabled below to provide temporary access details (wp-admin and FTP).

I have set the next reply to private which means only you and I have access to it.

#380112

Minesh
Supporter

Languages: English (English )

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

I think you forget this:
Could you please share link of the page where you've added CRED form.

#380113

Register Your LittleChamp
use the join code as "SWMP12"
It will then take you to the page.
or
hidden link

#380121

Minesh
Supporter

Languages: English (English )

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

Could you please check now:

we were missing $form_data argument to function. I've added following code to your functions.php file.

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

// if a specific form
if ($form_data['id']==87)  {
     
        $fname= get_post_meta($post_id, 'wpcf-first-name', true);
        $sname= get_post_meta($post_id, 'wpcf-surname', true);
	$title = $fname."-". $sname;

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

I hope above solution will help you to resolve your issue.

#380361

Thanks so much. Great result. I didn't pick it up either.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.