I’m trying to save some data from a form created in WPForms into a tab within the user information in WooCommerce. I can create the new tab I want inside the profile and the corresponding labels where the information is supposed to be stored, but every time I fill out the form, the data is not being saved.
The code I’m using is as follows:
PHP to retrieve the data:
// Hook para ejecutar cuando se envíe el formulario
function guardar_datos_wpforms($entry, $form_data) {
$form_id = 123; // Cambia esto al ID de tu formulario WPForms
if ($form_data['id'] == $form_id) {
$user_id = get_current_user_id();
if ($user_id) {
// Obtener datos del formulario
$fields = $entry['fields']; // Campos del formulario
$nombre_mascota = isset($fields['1']) ? sanitize_text_field($fields['1']['value']) : '';
// Registrar datos para depuración
error_log('Formulario ID: ' . $form_id);
error_log('Usuario ID: ' . $user_id);
error_log('Nombre de la Mascota: ' . $nombre_mascota);
// Guardar en el perfil del usuario
update_user_meta($user_id, 'nombre_mascota', $nombre_mascota);
}
}
}
add_action('wpforms_process_complete', 'guardar_datos_wpforms', 10, 2);
PHP to save the data:
// Guardar la información de la pestaña 'Mascota'
function guardar_pestaña_mascota($user_id) {
// Verificar que el usuario tiene permisos para editar su propio perfil
if (!current_user_can('edit_user', $user_id)) {
return false;
}
// Verificar que los datos están presentes en $_POST
if (isset($_POST['nombre_mascota'])) {
update_user_meta($user_id, 'nombre_mascota', sanitize_text_field($_POST['nombre_mascota']));
}
}
add_action('personal_options_update', 'guardar_pestaña_mascota');
add_action('edit_user_profile_update', 'guardar_pestaña_mascota');
PHP to create the tab and store the information there:
// Añadir la pestaña 'Mascota' al perfil del usuario
function agregar_pestaña_mascota($user) {
?>
<h3><?php _e('Información de la Mascota', 'twentytwentyfour'); ?></h3>
<table class="form-table">
<tr>
<th><label for="nombre_mascota"><?php _e('Nombre del Perro'); ?></label></th>
<td>
<input type="text" name="nombre_mascota" id="nombre_mascota" value="<?php echo esc_attr(get_the_author_meta('nombre_mascota', $user->ID)); ?>" class="regular-text" />
</td>
</tr>
</table>
<?php
}
add_action('show_user_profile', 'agregar_pestaña_mascota');
add_action('edit_user_profile', 'agregar_pestaña_mascota');
I’m using WPForms and the ID it generates in the builder.