Skip Navigation

[Resolved] Autopopulate text field from title

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

Problem:
Autopopulate custom text field from title

Solution:
If you are using Toolset form then you will require to use "cred_save_data" hook to copy the title to custom field. If you want to copy the title to custom filed when you save the post from backend admin, you can use the "save_post" hook.

You can find the proposed solution in this case with the following reply:
https://toolset.com/forums/topic/autopopulate-text-field-from-title/#post-2258915

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

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

Last updated by simchaH 2 years, 11 months ago.

Assisted by: Minesh.

Author
Posts
#2258621

Hi,

I found this thread that seems to be for the most part the solution to my inquiry:

https://toolset.com/forums/topic/auto-populate-a-text-area-field-from-another-custom-field/

My only difference is that I want to populate a text field from the title of the post (as opposed to from another custom field). Is there going to be a unique separate syntax for populating from the post-title of my CPT?

Thanks!

#2258885

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

I understand that you want to copy post title to another custom field but can you please tell when you want to do that. When using Toolset post form or when you save the post from backend?

#2258907

Well I would primarily like to do it both ways, for admin in backend and for when users on the frontend submit a new post... I saw in the thread there were both options there.

#2258915

Minesh
Supporter

Languages: English (English )

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

- Using Toolset form:

add_action('cred_save_data', 'copy_title_to_custom_field',10,2);
function copy_title_to_custom_field($post_id, $form_data) {
    // if a specific form
    if ($form_data['id']==9999 ){
         $title = get_the_title( $post_id );
         update_post_meta($post_id, 'wpcf-your-field-slug', $title );
        
    }
}

- Where replay 9999 with your original form ID
- replace the "your-field-slug" with your original field slug.

- Using post add/edit screen on backend admin

add_action( 'save_post', 'copy_title_to_custom_field_admin', 100, 3 );
function copy_title_to_custom_field_admin( $post_id, $post, $update ) {
  if ( $post->post_status == 'publish' && $post->post_type == 'cpt-slug' ) {
   
        $title = $post->post_title;
         update_post_meta($post_id, 'wpcf-your-field-slug', $title );
  }
}

Where:
- replace "cpt-slug" with your original post type slug
- replace the "your-field-slug" with your original field slug.

#2258917

Thank you Minesh! That was super quick and helpful.

My issue is resolved now. Thank you!