Skip Navigation

[Resolved] adding two custom fields together to make a post title on submit

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.

You can find proposed solution with the following reply:
https://toolset.com/forums/topic/adding-two-custom-fields-together-to-make-a-post-title-on-submit/#post-412305

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

This support ticket is created 7 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
- 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 2 replies, has 2 voices.

Last updated by KarlK9218 7 years, 9 months ago.

Assisted by: Minesh.

Author
Posts
#412229

I am trying to:

add two user inputs "first-name" and "last-name" to use as the post-title for a CRED post form. I tried looking at the CRED API but I not understanding where the code goes in the form HTML in the Toolset/Post Forms.

I've seen two others ask the similar questions. They are probably more advanced than I am.

Should the "add_action("cred_save_data", "my_save_data_action",10,2);
function my_save_data_action($post_id, $form_data)...." items be wrapped in php tags since they are going into an HTML text area? I would think so.

I added the prefix "wpcf" to my custom field names "first-name" and "last-name" in the Post Form, however, I am not sure if that is right because the form input don't have the prefix. The data hasn't been processed yet. I tried it both ways and I just get a default "Auto Draft" post name.

Maybe I am placing the code in the wrong area?

Thanks for your help.

#412305

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

You need to add the following code to current theme's functions.php file.

add_action('cred_save_data','func_custom_post_title',10,2);
 function func_custom_post_title($post_id,$form_data) {
    $type = get_post_type($post_id);
    if ($form_data['id']==9999)
    {
        $c1 = get_post_meta($post_id, 'wpcf-first-name', true);
        $c2 = get_post_meta($post_id, 'wpcf-last-name', true);
       
 
       $title= $c1. ' ' . $c2;
 
        $slug = sanitize_title($title);
 
 
           $args = array('ID' => $post_id, 
                                   'post_title' => $title, 
                                   'post_name' => $slug
                          );
 
        wp_update_post($args);
    }
}

Where:
Replace '9999' with original CRED form ID.
Replace your custom field names - if needed if they are not correct

More info:
https://toolset.com/documentation/user-guides/cred-api/#csd

Could you please use above code and try to resolve your issue.

#412531

Hi Minish

That worked great, didn't have to turn off Auto Draft.

Thanks so much!

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