Attenzione!

Questo articolo contiene codice PHP ed è destinato agli sviluppatori. Offriamo questo codice come cortesia, ma non forniamo supporto per personalizzazioni del codice o sviluppo di terze parti.

Per ulteriore assistenza, consulta il tutorial di WPBeginner su come aggiungere codice personalizzato.

Chiudi

Descrizione

Il filtro wpforms_process_redirect_url viene attivato all'invio del modulo per caricare l'URL necessario per il reindirizzamento.

Parametri

$url
(string) URL per il reindirizzamento.
$form_id
(int) ID del modulo.
$form_data
(array) Impostazioni/dati del modulo elaborati, preparati per essere utilizzati in seguito.
$fields
(array) Dati del campo sanificati.
$entry_id
(int) ID della voce.

Origine

wpforms/includes/class-process.php

Maggiori Informazioni

Il filtro wpforms_process_redirect_url che verrà utilizzato per il reindirizzamento una volta inviato il modulo.

Esempi

In questo esempio, configureremo il modulo per un reindirizzamento nelle impostazioni del costruttore di moduli, quindi in base a una domanda specifica sul modulo, reindirizzeremo l'utente a URL diversi in base alla risposta.

Se hai intenzione di utilizzare questo snippet, è importante ricordare che dalla scheda Impostazioni, nella sezione Conferme, imposta il Tipo di conferma su Vai all'URL (Reindirizza). Per ulteriori informazioni a riguardo, consulta questa documentazione.

/**
 * 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.