Skip Navigation

[Résolu] before_save_data with User Form

This support ticket is created Il y a 5 années et 2 mois. 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 11 réponses, a 3 voix.

Dernière mise à jour par poM Il y a 5 années et 2 mois.

Assisté par: Minesh.

Auteur
Publications
#1338527

How do I implement a before_save_data hook for a User edit form please?

I've tried adjusting the hook format for a Custom Post edit form from this

add_action('cred_before_save_data', 'ts_before_save_data_form_1990',10,2);
function ts_before_save_data_form_1990($form_data) {
    
    if ($form_data['id']==1990) {
        
       $post_id = $form_data['container_id'];
       //do something
	}
}

to this

add_action('cred_before_save_data', 'ts_before_save_data_form_1990',10,2);
function ts_before_save_data_form_1990($form_data) {
    
    if ($form_data['id']==1990) {
        
       $user_id = $form_data['container_id'];
       //do something
	}
}

and I've also tried this:-

add_action('cred_before_save_data', 'ts_before_save_data_form_1990',10,2);
function ts_before_save_data_form_1990($form_data) {
    
    if ($form_data['id']==1990) {
        
       $user_id = get_the_id();
       //do something
	}
}

but neither are working.

In my scenario, I do need the ID of the User's Profile ($user_id) NOT the ID of the current user (the user submitting the form).

#1338579

Minesh
Supporter

Les langues: Anglais (English )

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

Hello. Thank you for contacting the Toolset support.

I would like to know, how you added the edit profile form? Using view? can you please share how you added edit profile form shortcode or you added edit profile form link?

#1338599

I've simply put the user form shortcode [cred_user_form form='form_name'] in the editor for the page. The page has a Content Template assigned to it which shows the page content to anyone but Guests using the Access shortcode like this:-

[toolset_access role="Guest" operator="deny"][wpv-post-body view_template="None" output="raw"][/toolset_access]

I did think I would need a View originally but it appears this isn't necessary (I assume the WordPress rules that a user can only update their own profile - unless they're an administrator - apply).

#1338605

Minesh
Supporter

Les langues: Anglais (English )

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

Ok - so, by default when you add Toolset User edit form, it will display the user form for currently logged-in user profile for edit.

Are you currently loggedin as admin or as different user?

#1338651

A different user won't see the contents of the edit form. The hook needs to differentiate between the ID of the Profile being updated and the ID of the user updating it (which will only be Site Admin of course). I don't want to hardcode Site Admin's User ID into the hook hence my question about how I retrieve the ID of the profile in a before_save_data hook.

#1338653

Minesh
Supporter

Les langues: Anglais (English )

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

I understand, but the default behaviour is if you logged in as admin and display the edit user form it will display the currently loggedin user profile form for editing.

I think you need to use edit user form link so that you can display the user form for editing for specific user of your choice. Can I have problem URL and access details so I can check how you configured your form and also tell me what user profile you want to edit with user edit form.

I have set the next reply to private which means only you and I have access to it.

#1338711

Hi Minesh

Sorry but the site is locked down so I can't give access.

Would it not just be easier and quicker all round to simply let me know to retrieve the id of the profile in a before_save_data hook and let me worry about the rest??

#1338715

Minesh
Supporter

Les langues: Anglais (English )

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

Actually, the thing is that, if you check the "cred_before_save_data" hook:
=> https://toolset.com/documentation/programmer-reference/cred-api/#cred_before_save_data

It only offers the following attributes:

form_id. The form ID.
post_type. The post type that the form operates on.
form_type. The type of the form (create or edit form.
container_id. Refers to the post_id of the post, page or custom post type that contains the CRED form

So basically, there is no user_id to get.

what if you try to add the generic hidden field to your form and assign its default value as your required user id and then try to grab this that on cred_before_save_data with $_POST.
=> https://toolset.com/documentation/user-guides/inserting-generic-fields-into-forms/

#1338989

Hi again

Well the documentation is poor but I believe the generic field needs to be added like this?:-

[cred_generic_field type='numeric' field='profileID']{"required":0,"default":"[wpv-user field="ID"]"}[/cred_generic_field]

and then the before_save_data hook would look like this?:-

add_action('cred_before_save_data', 'ts_before_save_data_form_1990',10,2);
function ts_before_save_data_form_1990($form_data) {
     
    if ($form_data['id']==1990) {
         
       $user_id = $_POST['profileID'];//generic field name
       //do something
    }
}

However I'm getting an Undefined Index error on 'profileID'. Can you point me in the right direction please?

#1339341

Minesh
Supporter

Les langues: Anglais (English )

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

I checked on my install, the same way, I've added the following hidden generic field on my test install.

[cred_generic_field type='hidden' field='user_profile_id']
{
"default":"[wpv-user field='ID']"
}
[/cred_generic_field]

And added the following hook and when I submit the form, I can see its displaying "user_profile_id".

add_action('cred_before_save_data', 'func_before_sd',10,1);
function func_before_sd($form_data){
    // if a specific form
    if ($form_data['id']==1990) {
        echo "<pre>";
        print_r($form_data);
     	 print_r($_POST);
        exit;
    }
}

Can you please confirm?

#1339419

Hi Minesh

I'm not sure what's happened but the generic field IS picking up the user id now so thank you for your help with this; much appreciated

#1393787

poM

FYI,

You can use the following to get the user ID modified by the Cred form

$user_id = $_POST['_cred_cred_prefix_post_id'];