¡Atención!

Este artículo contiene código PHP y está destinado a desarrolladores. Ofrecemos este código como cortesía, pero no proporcionamos soporte para personalizaciones de código o desarrollo de terceros.

Para obtener ayuda adicional, consulta el tutorial de WPBeginner sobre cómo añadir código personalizado.

Descartar

Descripción

El filtro wpforms_process_redirect_url se activa al enviar el formulario para cargar la URL necesaria para la redirección.

Parámetros

$url
(string) URL para la redirección.
$id_formulario
(int) ID del formulario.
$form_data
(array) Configuración/datos del formulario procesados, preparados para ser utilizados posteriormente.
$fields
(array) Datos del campo saneados.
$entry_id
(int) ID de la entrada.

Origen

wpforms/includes/class-process.php

Más Información

El filtro wpforms_process_redirect_url que se utilizará para la redirección una vez que se envíe el formulario.

Ejemplos

En este ejemplo, vamos a configurar el formulario para una redirección en la configuración del constructor de formularios y, a continuación, basándonos en una pregunta específica del formulario, redirigiremos al usuario a diferentes URL según la respuesta.

Si planea usar este fragmento, es importante recordar que, desde la pestaña Ajustes, en la sección Confirmaciones, establezca el Tipo de confirmación en Ir a URL (Redirigir). Para más información sobre esto, consulte esta documentación.

/**
 * Redirect URL.
 *
 * @link https://wpforms.com/developers/wpforms_process_redirect_url/
 *
 * @param string     $url       URL to redirect to.
 * @param int        $form_id   The form ID.
 * @param array      $form_data Processed form settings/data.
 * @param array      $fields    Sanitized fields data.
 * @param int        $entry_id  Entry id.
 *
 * @return string
 */

function wpf_dev_process_redirect_url( $url, $form_id, $fields, $form_data, $entry_id ) {

// Only run on my form with ID = 879.
    if ( absint( $form_data[ 'id' ] ) !== 879 ) {
        return $url;
    }
	// Assign the checkbox field that shows the room number to a variable
	$room_number = $fields[4][ 'value' ]; 

	if ($room_number === "Room A") {

		// Redirect to page with Room A information
		$url = 'http://myexamplesite.com/room-a-info';
	}
	    elseif ($room_number === "Room B") {

			// Redirect to page with Room B information
			$url = 'http://myexamplesite.com/room-b-info';
		}
		    elseif ($room_number === "Room C") {

			// Redirect to page with Room C information
			$url = 'http://myexamplesite.com/room-c-info';
		}
	    else {

			// Keep the current redirect URL that is set in the form builder settings
			$url = $url;
		}

    return $url;
}
add_filter( 'wpforms_process_redirect_url', 'wpf_dev_process_redirect_url', 10, 5 );

Using this snippet means that any URL you have set on the Confirmations tab will be ignored and utilize this redirect instead.

If the user selects to reserve Room A, the form will ignore the URL from the form builder’s redirect settings on the Confirmations tab and instead redirect to a URL containing information about Room A. The loop continues through the options listed for each room with a fallback in the final else and keeps the original URL from the form builder settings.