ご注意!

この記事には PHP コードが含まれており、開発者を対象としています。このコードは便宜上提供していますが、コードのカスタマイズやサードパーティの開発についてはサポートを提供していません。

追加のガイダンスについては、WPBeginner の カスタムコードの追加方法に関するチュートリアル を参照してください。

閉じる

説明

wpforms_process_redirect_url フィルターは、リダイレクトに必要な URL を読み込むために、フォーム送信時に実行されます。

パラメーター

$url
(string) リダイレクト先の URL。
$form_id
(int) フォーム ID。
$form_data
(配列) 後で利用できるように処理されたフォームの設定/データの配列。
$fields
(配列) サニタイズされたフィールドデータ。
$entry_id
(int) エントリ ID。

ソース

wpforms/includes/class-process.php

詳細情報

フォーム送信後にリダイレクトに使用される wpforms_process_redirect_url フィルター。

この例では、フォームビルダーの設定でリダイレクト用にフォームを設定し、次にフォームの特定の質問に基づいて、回答に応じてユーザーを異なる URL にリダイレクトします。

このスニペットを使用する場合は、設定タブの確認セクションで、確認タイプURL に移動 (リダイレクト) に設定することが重要です。詳細については、こちらのドキュメントをご覧ください

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