Skip Navigation

[Resolved] Cred_sava_data for user meta

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

Last updated by Pat 4 years, 3 months ago.

Assisted by: Minesh.

Author
Posts
#1447671

Pat

Hello,

I'm trying to use cred_save_data to update user meta after submitting a form.
Here is the code I'm using :

add_action('cred_save_data', 'adhesion_membre_modif',10,2);
function adhesion_membre_modif($post_id, $form_data)
{
// if a specific form
if ($form_data['id']==842)

{
$photo_membre = "";
$current_user = wp_get_current_user ();
$user_id = $current_user -> ID;
$nom_membre = $_POST['wpcf-nom'];
$prenom_membre = $_POST['wpcf-prenom'];
$photo_membre = $_POST['wpcf-photo-identite-membre'];

update_user_meta ($user_id, 'wpcf-photo', $photo_membre);
update_user_meta ($user_id, 'first_name', $prenom_membre);
update_user_meta ($user_id, 'last_name', $nom_membre);
}
}

The form (ID 842) has the following content :
[credform]
<div class="row">
<div class="col-sm-6">
[cred_field field='form_messages' class='alert alert-warning']
<div class="form-group">
<label>Civilité</label>
[cred_field field='civilites' force_type='field' class='form-control' output='bootstrap']
</div>
<div class="form-group">
<label>Votre Prénom</label>
(Ne sera jamais visible sur le site)
[cred_field field='prenom' force_type='field' class='form-control' output='bootstrap']
</div>
<div class="form-group">
<label>Votre Nom</label>
(Ne sera jamais visible sur le site)
[cred_field field='nom' force_type='field' class='form-control' output='bootstrap']
</div>
<div class="form-group">
<label>Une photo d'identité de vous</label>
[cred_field field='photo-identite-membre' force_type='field' class='form-control' output='bootstrap']
</div>
<div class="form-group">
<label>Votre date de naissance</label>
(Ne sera jamais visible sur le site. Sert uniquement à définir votre tranche d'âge)
[cred_field field='date-naissance-membre' force_type='field' class='form-control' output='bootstrap']
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label>Présentez-vous en quelques lignes</label>
[cred_field field='description-membre' force_type='field' class='form-control' output='bootstrap']
</div>

<div class="form-group cat-sport">
<label>Vos sports préférés</label>(plusieurs choix possibles)
[cred_field field='category' force_type='taxonomy' output='bootstrap' display='checkbox']
</div>

<div style="display:none;">
[cred_generic_field type='textfield' field='post_title']
{
"required":0,
"default":"[wpv-user [wpv-user field="user_login"]"
}
[/cred_generic_field]

[cred_generic_field type='numeric' field='wpcf-id-utilisateur']

{
"required":0,
"persist":1,
"validate_format":0,
"default":"[wpv-user field="ID"]"
}
[/cred_generic_field]
</div>
<div class="row col-sm-12">
[cred_field field='form_submit' output='bootstrap' value='Envoi' class='et_pb_button']
</div>
</div>
</div>
[/credform]

No errors are reported but no info is stored as user meta.
Any idea of what could be wrong in this code?
Regards
Pat

#1448039

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Based on the code you shared I think I spot the issue.

Can you please try to use the following code and try to resolve your issue:

add_action('cred_save_data', 'adhesion_membre_modif',10,2);
function adhesion_membre_modif($post_id, $form_data) {
// if a specific form
if ($form_data['id']==842) {
$photo_membre = "";
$user_id =$post_id;   // when using user forms $post_id will be actually your $user_id
$nom_membre = $_POST['wpcf-nom'];
$prenom_membre = $_POST['wpcf-prenom'];
$photo_membre = $_POST['wpcf-photo-identite-membre'];

update_user_meta ($user_id, 'wpcf-photo', $photo_membre);
update_user_meta ($user_id, 'first_name', $prenom_membre);
update_user_meta ($user_id, 'last_name', $nom_membre);
}
}

Also, I shared the solution yesterday and its working:
=> https://toolset.com/forums/topic/cred-modification-form-on-current-user-post-author/

Can you please confirm that solution I shared works for you as well.

#1448257

Pat

Hi Minesh,

Just tested it and this is still not working.
For info, I have placed the form in the "my-account" page, so not on a user dedicated page. That's why I have placed in the initial code some lines to retrieve the current logged in user.
I have also tried to place "$user_id = 1;" in order to be sure to have to right user (ie : admin when I'm making some tests) and still not working !
Regards
Pat

#1448321

Minesh
Supporter

Languages: English (English )

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

Can you please share the problem URL where you added the form and access details so I can check further.

*** 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 have set the next reply to private which means only you and I have access to it.

#1448453

Minesh
Supporter

Languages: English (English )

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

The thing is that you are using the post form and you want to update the user fields from post form hook.

So, I checked the code snippet "update-photo-user" you added to the Custom code section:
=> hidden link
- First thing the code snippet was inactive, so I've activated it
- then I've adjusted the code as given under:

add_action('cred_save_data', 'adhesion_membre_modif',10,2);
function adhesion_membre_modif($post_id, $form_data) {
// if a specific form
if ($form_data['id']==842) {
$photo_membre = "";
$current_user = wp_get_current_user();
$user_id = $current_user -> ID;
$nom_membre = $_POST['wpcf-nom'];
$prenom_membre = $_POST['wpcf-prenom'];
$photo_membre = $_POST['wpcf-photo-identite-membre'];

update_user_meta ($user_id, 'wpcf-photo', $photo_membre);
update_user_meta ($user_id, 'first_name', $prenom_membre);
update_user_meta ($user_id, 'last_name', $nom_membre);
}
}

Then, I changed the first name and last name with the following page:
=> hidden link

And when I visited the loggedin user profile page, I can see the firstname and last names are updated accordingly at top of the profile page:
=> hidden link

Do you want to update any other field?

#1448517

Pat

Hi Minesh,

Good progress, thank you.

A last point is that the photo field seems not updated in the user. My feeling is that what is stored inside is only the file name (not the complete url to get the file !).

Could you have a look on this please?
Regards
Pat

#1448519

Pat

Just a complementary info : when you save the form the first time, the photo field is not saved (only the file name). If you save the same form a second time, then, everything is fine?
Any idea?
Regards
Pat

#1448571

Minesh
Supporter

Languages: English (English )

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

Can you please check now: hidden link

I've just changed the following line of code from:

///$photo_membre = $_POST['wpcf-photo-identite-membre'];

To

$photo_membre=get_post_meta($post_id,'wpcf-photo-identite-membre',ture);

I can see now the photo is also changed. Can you please confirm.

#1448795

Pat

Thanks you Minesh.

That's working fine now.
Regards
Pat

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