2

クライアントの 1 人のために Web サイトを構築していますが、クライアントは次のような機能を Web サイトに追加したいと考えています。

ユーザーがダウンロード リンクをクリックすると、フォームが表示され (コンタクト フォーム 7)、訪問者が詳細を入力すると、ダウンロード リンクにリダイレクトされます。

お問い合わせフォーム7に以下の追加設定をすることで、フォーム送信後に新しいページにリダイレクトすることができました。

on_sent_ok: "location = 'http://example.com/';"

ここに画像の説明を入力

ただし、10 個のファイルがあります。適切なファイルのダウンロードをトリガーするには、リダイレクト リンクを 10 回変更する必要があります。非常に汚れる10個の連絡フォームを使用してそれを行うことができます.

リダイレクト URL を動的に変更する方法はありますか?

例えば、

http://example.com/?id=1
http://example.com/?id=2

<?php

$id = $_GET['id'];

$url= "http://example.com/id=?". $id; 


?>

次の Location を $url で変更する方法はありますか?

on_sent_ok: "location = 'http://example.com/';"
4

5 に答える 5

9

リダイレクト URL を動的に変更する方法を見つけました。動的リダイレクトを実現するために、次の手順に従いました。

  1. contact form 7 の追加設定に次のように入力します。

    on_sent_ok: 'リダイレクト();'

  2. 必要な情報を運ぶための隠しフィールドが必要です。ただし、Contact form 7 ではデフォルトで隠しフィールドを作成できません。開発者 SevenSpark は、コンタクト フォーム 7 の非表示フィールドを許可する拡張機能を開発しました。 http://wordpress.org/extend/plugins/contact-form-7-dynamic-text-extension/ プラグインをダウンロードしてインストールしてください。コンタクト フォーム 7 用に 2 つの新しいタグが生成されていることがわかります。これにより、$_GET 変数から値を取得できるようになります。プラグインのページで詳細を確認してください。

    元。http://example.com/?foo= "バー"

  3. テンプレート ページを作成するか、ページ テンプレートを終了します。

  4. テンプレートを適切なページに割り当てます。デフォルトのテンプレートを使用する場合は、テンプレートを作成または割り当てる必要はありません。

  5. エディターでテンプレート ファイルを開きます。

  6. 次のコードを貼り付けます。

    <script>    
    
        function redirect() {
    
    
    
            // your hidden field ID
               var filename = document.getElementById('redFilename').value;
    
            var url ='';
    
            //alert(document.getElementById('redFilename').value);
            if (filename == 'apple')
            {
    
    
                url= 'http://example.com/thankyou/';
    
    
            }
            else if (filename == 'orange')
            {
             url= 'http://example.com/thankyou_orange/';
            }    
    
    
     window.location = url;
    
            }
            </script>
    
  7. 次に、GET パラメータを使用してリンクを参照します。

    元。http://example.com/?redFilename= "りんご"

連絡先フォーム 7 の非表示フィールドは redFilename 値を取得します。フォームの送信が成功すると、http://example.com/thankyou_orange/ページにリダイレクトされます

楽しみ!!!!

于 2013-05-15T06:58:48.703 に答える
0

私はJavascriptルートをたどりましたが、これは機能します...しかし、送信された変数にアクセスすることはできません(少なくとも、どのようにアクセスするかわかりませんでした)。ここでは、Javascript コードを PHP コードに移植しました。

フォーム内の非表示または表示された入力にアクセスし、それらを使用して URL を作成できます。

Javascript ではなく PHP でリダイレクトを行うために必要なトリックが 1 つあります。それは、doc に従って CF7 Javascript をオフにする必要があることです。これをあなたの中に入れてくださいwp-config.php

define('WPCF7_LOAD_JS', false); 

次に、これをテーマに入れることができますfunctions.php

add_action( 'wpcf7_mail_sent', 'icc97_so_mail_sent', 10, 3);

/**
 * Ported Javascript code to redirect the CF7 form
 *
 * Have to do it in PHP because we want access to the POSTed data
 *
 * There is a further complication that the default CF7 submission is AJAX
 * Then our WP redirect doesn't do anything
 * So we have to turn off the CF7 Javascript via wp-config.php
 *
 */
function icc97_so_mail_sent( $contact_form ) {
    $submission = WPCF7_Submission::get_instance();
    $data = $submission->get_posted_data();
    // example data:
    // {"_wpcf7":"11684","_wpcf7_version":"4.9","_wpcf7_locale":"en_GB","_wpcf7_unit_tag":"wpcf7-f11684-p11681-o1","_wpcf7_container_post":"11681","your-name":"Ian","your-organisation":"Stack Overflow","your-email":"ian@example.com","your-agreement":"1"}

    /**
     * Get an attribute value from the CF7 form
     *
     * @param  string $name attribute name
     * @return string       attribute value
     */
    $attr = function($name) use ($data) {
        $val = $data[$name];
        // Dropdown / select values are arrays could be a one element array
        return is_array($val) ? $val[0] : $val;
    };

    /**
     * Create URL for downloads page
     *
     * @param  string $domain e.g. https://example.com, can be blank for relative
     * @param  array  $names  attributes
     * @return string         URL e.g. https://example.com/downloads/x/x/x/?data=xxx
     */
    $buildUrl = function ($domain, $names) use ($attr) {
        $stub = [$domain, 'downloads'];
        // we want lower case, attributes
        $path = array_map('strtolower', array_map($attr, $names));

        // create an array of the full URL and then join with '/'
        return join('/', array_merge($stub, $path));
    };

    $domain = '';
    // this requires AJAX to be turned off - see function doc block
    \wp_redirect($buildUrl($domain, ['your-name', 'your-organisation']));
    // exit otherwise CF7 forces it's own redirect back to the original page
    exit;
}
于 2017-09-13T19:38:08.917 に答える