Sauter la navigation

[Résolu] On submit form. Take the value from a field to another field.

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

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

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

Dernière mise à jour par jesusM-6 Il y a 3 années et 5 mois.

Assisté par: Shane.

Auteur
Publications
#2127035

Hello!

With the next code, in the WordPress admin, I can generate the value of the field "email-comercial-asociado" depending on the relationship field "comercial-expediente". But now, I want to do the same but when creating the post from a front-end form.

add_action( 'save_post', 'copy_relation_to_field_email_for_recomendado', 100, 3 );
function copy_relation_to_field_email_for_recomendado( $post_id, $post, $update ) {
if ( $post->post_status == 'publish' && $post->post_type == 'recomendado' ) {
$recomendado_id = $post_id;
// Get ID from CPT "comercial" parent of CPT "recomendado"
$comercial_id = toolset_get_related_post( $recomendado_id, 'comercial-expediente', 'parent');
$author_comercial_id = get_post_field( 'post_author', $comercial_id );
$author_coemrcial_email = get_the_author_meta( 'email', $author_comercial_id );
// Update custom field
update_post_meta( $post_id, 'wpcf-email-comercial-asociado', $author_coemrcial_email );
}
}

Thanks!

#2127123

Shane
Supporter

Les langues: Anglais (English )

Fuseau horaire: America/Jamaica (GMT-05:00)

Hi Jesus,

Thank you for getting in touch. If the above code works on the backend, then the only thing you need to do is to use the Hook for our frontend form.

The Hook that is equivalent to this would be the one in the link below.
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

Thanks,
Shane

#2127737

The first code run ok, but I modify it to use it with the front-end form and it doesn't work:

But I modify it to use it from the form and it doesn't work:

add_action('cred_save_data', 'copy_relation_to_field_email_for_recomendado_frontend',999,2);
function copy_relation_to_field_email_for_recomendado_frontend($post_id, $form_data)
{
// if a specific form
if ($form_data['id']==335)
{
if (isset($_POST['@comercial-expediente.parent']))
{
$recomendado_id = $post_id;
// Cogemos el ID del CPT comercial padre del recomendado
$comercial_id = toolset_get_related_post( $recomendado_id, 'comercial-expediente', 'parent');
$author_comercial_id = get_post_field( 'post_author', $comercial_id );
$author_coemrcial_email = get_the_author_meta( 'email', $author_comercial_id );
// add it to saved post meta
add_post_meta($post_id, 'wpcf-email-comercial-asociado', $author_coemrcial_email);
}
}
}

#2127919

Shane
Supporter

Les langues: Anglais (English )

Fuseau horaire: America/Jamaica (GMT-05:00)

Hi Jesus,

I'm not seeing any reason why this shouldn't work.

Would you mind allowing me to have admin access to the site as well as a link to the page where you are testing this so that I can do a firsthand check ?

Thanks,
Shane

#2128011

I have already found the error. The line of code to verify that the relationship field is defined is not correct.

if (isset($_POST['@comercial-expediente.parent']))

If I delete the line the field change is done correctly, but I need to evaluate if the relationship field is defined to avoid errors.

I need to know how the relationship field value is stored in the variable $ _POST

Any ideas to fix it? Thanks!

#2128019

Shane
Supporter

Les langues: Anglais (English )

Fuseau horaire: America/Jamaica (GMT-05:00)

Hi Jesusm

I believe the . is replaced by an underscore.

Try using this below.

if (isset($_POST['@comercial-expediente_parent']))

If the above doesn't work then please try the one below.

if (isset($_POST['@comercial-expediente-parent']))

Failing both of these you can dump the post_data variable unto the page when you submit the form

var_dump($form_data);

Then you should be able to see the entire payload of the form data and see exactly what exactly is the identifier for the field.

Thanks,
Shane

#2128021

Shane
Supporter

Les langues: Anglais (English )

Fuseau horaire: America/Jamaica (GMT-05:00)

Hi Jesusm

I believe the . is replaced by an underscore.

Try using this below.

if (isset($_POST['@comercial-expediente_parent']))

If the above doesn't work then please try the one below.

if (isset($_POST['@comercial-expediente-parent']))

Failing both of these you can dump the post_data variable unto the page when you submit the form

var_dump($form_data);

Then you should be able to see the entire payload of the form data and see exactly what exactly is the identifier for the field.

Thanks,
Shane

#2128027

My issue is resolved now. Thank you!