Skip Navigation

[Resuelto] Auto-populate a text area field from another custom field

Este hilo está resuelto. Aquí tiene una descripción del problema y la solución.

Problem: I would like to automatically copy an address custom field value into another textarea custom field when the post is saved.

Solution: It depends on whether the post is saved with CRED or if the post is saved in wp-admin. If CRED, you can use the cred_save_data hook:

add_action('cred_save_data', 'copy_location_to_multiline_field',10,2);
function copy_location_to_multiline_field($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==12345)
    {
        if (isset($_POST['wpcf-location']))
        {
            // add it to saved post meta
            update_post_meta($post_id, 'wpcf-multiline', $_POST['wpcf-location']);
        }
    }
}

Replace 12345 with the CRED form ID, and replace 'wpcf-location' and 'wpcf-multiline' with the correct field slugs. Add the 'wpcf-' prefix to the slug shown in wp-admin for each field.

If they are created in wp-admin, you can use the save_post hook:

add_action( 'save_post', 'copy_location_to_multiline_field_admin', 100, 3 );
function copy_location_to_multiline_field_admin( $post_id, $post, $update ) {
  if ( $post->post_status == 'publish' && $post->post_type == 'cptslug' ) {
    $location = get_post_meta( $post_id, 'wpcf-location', true);
    update_post_meta( $post_id, 'wpcf-multiline', $location );
  }
}

Replace 'cptslug' with the slug of the post type where you have your address field, and replace 'wpcf-location' and 'wpcf-multiline' with the correct field slugs. Add the 'wpcf-' prefix to the slug shown in wp-admin for each field.

Relevant Documentation: https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
https://codex.wordpress.org/Plugin_API/Action_Reference/save_post

This support ticket is created hace 7 años. 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.

Hoy no hay técnicos de soporte disponibles en el foro Juego de herramientas. Siéntase libre de enviar sus tiques y les daremos trámite tan pronto como estemos disponibles en línea. Gracias por su comprensión.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Este tema contiene 2 respuestas, tiene 2 mensajes.

Última actualización por Akhil hace 7 años.

Asistido por: Christian Cox.

Autor
Mensajes
#590922

Hi there, i am in need of this after working on the site for some time,

i have two field,
1. address lines
2 , address for map

field no 2 is autogenerated from the googlemap as i type.
i need the field no 2 value to be copied/populate to field no 1 .

i know this is possible as there is some code but those are for select field. mine is simple text field.

appreciate all assitance.thanks

#591045

Hello, are these posts created with CRED, or created in wp-admin? If CRED, you can use the cred_save_data hook:

add_action('cred_save_data', 'copy_location_to_multiline_field',10,2);
function copy_location_to_multiline_field($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==12345)
    {
        if (isset($_POST['wpcf-location']))
        {
            // add it to saved post meta
            update_post_meta($post_id, 'wpcf-multiline', $_POST['wpcf-location']);
        }
    }
}

Replace 12345 with the CRED form ID, and replace 'wpcf-location' and 'wpcf-multiline' with the correct field slugs. Add the 'wpcf-' prefix to the slug shown in wp-admin for each field.

If they are created in wp-admin, you can use the save_post hook:

add_action( 'save_post', 'copy_location_to_multiline_field_admin', 100, 3 );
function copy_location_to_multiline_field_admin( $post_id, $post, $update ) {
  if ( $post->post_status == 'publish' && $post->post_type == 'cptslug' ) {
    $location = get_post_meta( $post_id, 'wpcf-location', true);
    update_post_meta( $post_id, 'wpcf-multiline', $location );
  }
}

Replace 'cptslug' with the slug of the post type where you have your address field, and replace 'wpcf-location' and 'wpcf-multiline' with the correct field slugs. Add the 'wpcf-' prefix to the slug shown in wp-admin for each field.

#592326

Thank you Christian.