Skip Navigation

[Resolved] Cant save generic field data to custom user meta field

This support ticket is created 6 years, 7 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
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 4 replies, has 2 voices.

Last updated by Nigel 6 years, 7 months ago.

Assisted by: Nigel.

Author
Posts
#901828

I am trying to: save data entered on generic field to custom user meta field

I am using the following function to save data from a generic field to a custom user meta field

add_action('cred_save_data', 'user_imob_func',10,2);
function user_imob_func($user_id, $form_data)
{
// if a specific form
if ($form_data['id']==682)
{
if (isset($_POST['imobSelect']))
{
// add it to saved user meta
update_user_meta($user_id, 'wpcf-imobiliaria', $_POST['imobSelect'], true);
}
}
}

The field is a select field populated by a view listing a post type called Imobiliária.

The new user is created, but the Imobiliaria field is empty.

Cant figure out how to solve it.

#901840

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Marcus

I just ran a check to confirm that there are no issues updating user meta via a generic field on a test site and it worked fine.

Here was the code I used:

add_action( 'cred_save_data', 'tssupp_update_user', 10, 2 );
function tssupp_update_user( $user_id, $form_data ) {

    // check which form
    if ( $form_data['id'] == '55' ) {

    	error_log(print_r($_POST['favourite-team'], true));
     
     	if ( isset( $_POST['favourite-team'] ) ) {

	    	update_user_meta( $user_id, 'favourite-team', $_POST['favourite-team'] );
     	}
    }
}

I had a generic select field where the options were set manually when inserting it into the form.

I suspect the issue may be that your select field is not formed correctly and so the value is not submitted with the form as expected.

Note in my example I am dumping the $_POST object field to the error log just so that I can confirm what was submitted, and I suggest you do the same.

If you haven't already, turn on the debug log by editing your wp-config.php file and change the line with WP_DEBUG like so:

define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);
define('WP_DEBUG_DISPLAY', false);

That will create a debug file called debug.log in your wp-content folder which you can examine in any text editor, and you should see the expected value submitted with the form there.

If not the problem is earlier, i.e. in creating your generic field in the first place.

Let me know what you find.

#902622

Thanks for the reply. I activated the debug as you told me.

On the debug file, the options selected on the form are shown:

[23-May-2018 20:03:59 UTC] osmar-pinto
[23-May-2018 20:03:59 UTC] produto-teste

The code of the form is the following:

[creduserform class='cred-user-form cred-keep-original']

	[cred_field field='form_messages' value='']

    <div class="cred-field cred-field-first_name">
		<label class="cred-label">Nome</label>
		[cred_field field='first_name' post='user' value='' urlparam='']
	</div>

	<div class="cred-field cred-field-last_name">
		<label class="cred-label">Sobrenome</label>
		[cred_field field='last_name' post='user' value='' urlparam='']
	</div>

    <div class="cred-field cred-field-user_email">
		<label class="cred-label">E-mail</label>
		[cred_field field='user_email' post='user' value='' urlparam='']
	</div>

	<div class="cred-field cred-field-imobiliaria">
		<label class="cred-label">Imobiliária</label>
        [cred_generic_field field="imobSelect" type="select" class="" urlparam=""]
			{
				"required":1,
				"validate_format":0,
 				"persist":1,
				"default":"",
				"options":[[wpv-view name='imobiliarias-select']]
			}
		[/cred_generic_field]


	</div>

	<div class="cred-field cred-field-empreendimento">
		<label class="cred-label">Empreendimento</label>
 
        [cred_generic_field field="imobSelect2" type="select" class="" urlparam=""]
			{
				"required":1,
				"validate_format":0,
 				"persist":1,
				"default":"",
				"options":[[wpv-view name='empreendimentos-select']]
			}
		[/cred_generic_field]




	</div>

	<div class="cred-field cred-field-user_pass">
		<label class="cred-label">Senha</label>
		[cred_field field='user_pass' post='user' value='' urlparam='']
	</div>

	<div class="cred-field cred-field-user_pass2">
		<label class="cred-label">Repetir Senha</label>
		[cred_field field='user_pass2' post='user' value='' urlparam='']
	</div>


	[cred_field field='form_submit' value='Enviar' urlparam='']

[/creduserform]

The code of the view that generate the select items of the first selector is the following (the second uses the same logic):

[wpv-layout-start]
	[wpv-items-found]
	<!-- wpv-loop-start -->
	<wpv-loop>
		[wpv-item index=1]
        {"value":"[wpv-post-slug]","label":"[wpv-post-title]"}
        [wpv-item index=other]
        ,{"value":"[wpv-post-slug]","label":"[wpv-post-title]"}
	</wpv-loop>
	<!-- wpv-loop-end -->
	[/wpv-items-found]
	[wpv-no-items-found]
		<strong>[wpml-string context="wpv-views"]No items found[/wpml-string]</strong>
	[/wpv-no-items-found]
[wpv-layout-end]

The users are created, but the values of the generic fields are not saved.

#902626

Important: I am trying to save the generic field value on an existing custom user field.

#902787

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Marcus

I just double-checked this worked on my test site and didn't experience any problems.

I also tried an alternative. Because you want to save the custom user field when the form is submitted you can take advantage of the option "persist":1 which you need to manually add to your generic field for it to be saved to the database.

So it would look something like this:

[cred_generic_field field='wpcf-team' type='select' class='' urlparam='']
{
"required":0,
"validate_format":0,
"default":[],
"options":[
{"value":"liverpool","label":"Liverpool"},
{"value":"liverpoolfc","label":"Liverpool FC"},
{"value":"lfc","label":"LFC"}
],
"persist":1
}
[/cred_generic_field]

That will automatically save the field as user meta (note you need to include the wpcf- prefix if you are saving a Types field.

So that's an alternative you could try.

With your current set up, when you say the field isn't saved, what kind of field is the corresponding Types field (wpcf-imobiliaria)?

If it were a select field where you had defined several options with values & labels and the value stored in the database didn't match the value where you defined the custom user field then when looking at the user profile it would appear that nothing was selected, even though something was saved in the database.

So I would inspect the wp_usermeta table to see whether a wpcf-imobiliaria field had been saved, but with a non-matching value.