Skip Navigation

[Résolu] How to populate user name and surname as post title in a cred form

This support ticket is created Il y a 5 années. 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.

Aucun de nos assistants n'est disponible aujourd'hui sur le forum Jeu d'outils. Veuillez créer un ticket, et nous nous le traiterons dès notre prochaine connexion. Merci de votre compréhension.

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)

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

Dernière mise à jour par israelP Il y a 5 années.

Assisté par: Minesh.

Auteur
Publications
#1378041

I have created a form with Cred for the post types to be filled in by registered users, but I need the post type to be filled in with the name and surname of the registered user.
At the moment, it appears in my post type as, for example, CRED Auto Draft f528764d624db129b32c21fbca0cb8d6.

#1378049

Minesh
Supporter

Les langues: Anglais (English )

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

Hello. Thank you for contacting the Toolset support.

Assuming you created custom user fields using Types:

You can try to 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/

 
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) {

           $current_user = wp_get_current_user();
            $user_id = $current_user->ID;

            $name = get_user_meta($user_id, 'wpcf-name', true);
            $surname = get_user_meta($user_id, 'wpcf-surname', true);

        
        $title= $name. '-' . $surname;
        $args = array('ID' => $post_id, 'post_title' => $title);
        wp_update_post($args);
    }
}

Where:
- Replace 99999 with your original form ID
- please change the user field slug if required

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

#1378153

Hello Minesh,

It works, since the wpcf-name and wpcf-surname are custom user fields created using Types. What about using the "name" and "surname" of the system (user elements)? What would be the code, please?

#1378563

Minesh
Supporter

Les langues: Anglais (English )

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

Do you mean that the current user's profile's First name and Last name? If yes:

You should try 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']==9999) {
 
           $current_user = wp_get_current_user();
            $user_id = $current_user->ID;
             
             // getting user info based on current user ID
            $user_info = get_userdata($user_id);
     
 
            $name = $user_info->first_name;
            $surname = $user_info->last_name;
 
         
        $title= $name. '-' . $surname;
        $args = array('ID' => $post_id, 'post_title' => $title);
        wp_update_post($args);
    }
}

Where:
- Replace 99999 with your original form ID

#1378651

My issue is resolved now. Thank you!

#1378747

I have 2 different credit forms, one records the data into post types of a role and the other one records the data into post types of a different role. The ID of the forms are 2859 and 2886. I have duplicated your code, chaning the ID of the form, but it does not work. How can I solve, please.

Thank you so much.

#1378753

Minesh
Supporter

Les langues: Anglais (English )

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

Please mark this ticket as resolved. I've split the ticket and I will reply there.

#1379573

My issue has been resolved. Thank you!