Passer la navigation

[Résolu] Autopopulate text field from title

Ce fil est résolu. Voici une description du problème et la solution proposée.

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 Il y a 4 years, 4 months. 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 -

Fuseau horaire du supporter : Asia/Kolkata (GMT+05:30)

Ce sujet contient 4 réponses, a 2 voix.

Dernière mise à jour par simchaH Il y a 4 years, 4 months.

Assisté par: Minesh.

Auteur
Publications
#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

Les langues: Anglais (English )

Fuseau horaire: 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

Les langues: Anglais (English )

Fuseau horaire: 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!