everything good? I have a WordPress site with Elementor Pro installed and I have been trying unsuccessfully to manipulate the data of a form and save the result of this manipulation in a hidden field on the form itself. Specifically, I need to create a record that has the prefix EXC, followed by the person’s first name and then the last four phone numbers. Ex: EXC Pedro 1234. The form has only 3 fields in addition to the hidden field. I’ve already tested 3 scripts but none worked, some with an error message after sending, others without an error message. In common, all were saved correctly, however, without the result of manipulation in the hidden field.
Could anyone help or guide me so that I can carry out this manipulation and recording? Thank you in advance!
The scripts used were the following:
add_action( 'elementor_pro/forms/new_record', function( $record, $handler ) {
// Certifique-se de que o formulário é o "cap-avalanche"
$form_name = $record->get_form_settings( 'form_name' );
if ( 'cap-avalanche' !== $form_name ) {
return;
}
$raw_fields = $record->get('fields');
$fields = [];
foreach ( $raw_fields as $id => $field ) {
$fields[ $id ] = $field['value'];
}
// Manipulação dos dados
if ( ! empty( $fields['name'] ) && ! empty( $fields['field_3aa9bef'] ) ) {
// Pega o primeiro nome
$nome_array = explode(' ', trim($fields['name']));
$primeiro_nome = $nome_array[0];
// Pega os quatro últimos números do telefone
$telefone = preg_replace('/[^0-9]/', '', $fields['field_3aa9bef']);
if(strlen($telefone) >= 4) {
$ultimos_quatro = substr($telefone, -4);
} else {
// Se o telefone tiver menos de 4 dígitos, usa o que estiver disponível
$ultimos_quatro = $telefone;
}
// Cria o novo título
$novo_titulo = 'FAD' . $primeiro_nome . $ultimos_quatro;
// Atualiza o campo oculto 'titulo'
$_POST['form_fields']['titulo'] = $novo_titulo;
}
}, 10, 2 );
In the next script I changed the way the record is updated, according to the Elementor documentation:
add_action( 'elementor_pro/forms/new_record', function( $record, $handler ) {
// Certifique-se de que o formulário é o "cap-avalanche"
$form_name = $record->get_form_settings( 'form_name' );
if ( 'cap-avalanche' !== $form_name ) {
return;
}
$raw_fields = $record->get('fields');
$fields = [];
foreach ( $raw_fields as $id => $field ) {
$fields[ $id ] = $field['value'];
}
// Manipulação dos dados
if ( ! empty( $fields['name'] ) && ! empty( $fields['field_3aa9bef'] ) ) {
// Pega o primeiro nome
$nome_array = explode(' ', trim($fields['name']));
$primeiro_nome = $nome_array[0];
// Pega os quatro últimos números do telefone
$telefone = preg_replace('/[^0-9]/', '', $fields['field_3aa9bef']);
if(strlen($telefone) >= 4) {
$ultimos_quatro = substr($telefone, -4);
} else {
// Se o telefone tiver menos de 4 dígitos, usa o que estiver disponível
$ultimos_quatro = $telefone;
}
// Cria o novo título
$novo_titulo = 'FAD' . $primeiro_nome . $ultimos_quatro;
// Atualiza o campo oculto 'titulo'
// Encontra o campo pelo seu ID e atualiza seu valor
foreach ($record->get('fields') as $field_id => &$field) {
if ('titulo' === $field_id) {
$field['value'] = $novo_titulo;
break;
}
}
}
// Atualiza os dados do formulário com os novos valores dos campos
$record->update_fields();
}, 10, 2 );
JuxCo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.