テーマのfunctions.phpに書いた関数内変数の受け渡し
-
お世話になっております。
現在ContactForm7を使っておりフォームを入力していただくと固定のサンクスページに自動でリダイレクトします。フォーム入力後、メール送信前(wpcf7_before_send_mail)に入力された値を別サービスにAPIを使ってPOSTしております。
POST後Json形式で識別コードが返されるのですが、その値をURLにパラメータとして付与してサンクスページへ遷移させたいのですがどうにもうまくいきません。(サンクスページのURL+’?c=’+識別コードの様な感じ)
APIでのPOST、固定ページへのリダイレクトは実現できていますが、識別コードの値をリダイレクトする関数に渡すという部分で行き詰まっております。WPもPHPも勉強し始めたばかりなのでお力をお貸していただけないでしょうか。
・利用ツール
CMS
WordPress(v.5.3.1)
フォーム
Contact Form 7(v.5.1.6※WordPressのプラグイン)
サーバー
レンタルサーバーの共有プランです。・やったこと、予想(コードにも記載)
POSTする関数にリダイレクトまでを含める
→ContactForm7がajaxを利用している為リダイレクトの様な挙動は干渉してしまう?
識別コードをhtmlに書き込む
→functions.phpで直接echoなどで出力できない用で書き込み方がわからない
識別コードをメールの内容やタグなどに含ませリダイレクトの関数の引数eventで取得
→これはシンプルにやり方(そもそも出来るのか)が分かりません
以下コード//WordPressのテーマのfunctions.phpに記述 add_action("wpcf7_before_send_mail","test1"); add_action("wp_footer","test2"); function test1($WPCF7_ContactForm){ //※フォームの値を取得してAPI用に整形する部分は省略 //POSTする処理 $base_url = 'https://api.satr.jp/api/v4/public/customer/upsert.json?'; $opts = array( 'http' => array( 'method' => "POST", 'header' => "Content-type: application/x-www-form-urlencoded\r\n" )); $context = stream_context_create($opts); $response = file_get_contents( $base_url . $post_string, false, $context ); //識別コードを変数($hashcode)に $result = json_decode($response, true); $user_data = array( 'status' => $result['status'], 'hashcode' => $result['message']['customer[hashcode]'] ); $hashcode = $user_data[hashcode]; $status = $user_data[status]; //html要素に含ませたい(失敗) $dom = new DOMDocument(); $dom->loadHTML(mb_convert_encoding('<p id="para2">ssssssssssssssss</p>')); $xml = $dom->saveXML(); $res = json_decode(json_encode(simplexml_load_string($xml)), true); $tes = $res['body']['h1'].textContent; //フォームと同一製作者のプラグイン「flamingo」に保存(失敗) $field = 'your-lastname'; $contactform = 'コンタクトフォーム1'; $fla = wpcf7_flamingo_get_value($field,$contactform); //メールでhashcodeが受け取れているか確認(成功。受け取れています) /*$mail = $wpcf7->prop('mail'); $mail['body'] =$hashcode; $wpcf7->set_properties(array( "mail" => $mail ));*/ //以下リダイレクト処理(失敗。コメントにエラー内容と予測) //header('Location: https://techacademy.jp/');//CORSらしい //header('Location: https://apiv4test.itage.jp/thanks/');//<div class="ajax-error">Unexpected token <; in JSON at position 0</div> //header("Location:$url");//<div class="ajax-error"></div> //wp_redirect($url,301); //ajaxの動作目的を鑑みるとリダイレクトは恐らく不可 //どこかでhtml箱で作って結果ハッシュコードの値をhtml箱に入れる? return $hashcode; } //固定ページリダイレクト function test2(){ global $hashcode; //htmlの要素であれば呼べる筈 ?> <script> document.addEventListener('wpcf7mailsent', function(event) { //phpのグルーバル変数を使うことも考慮したがサーバーサイドで持っている値$hashcodeをフロント側で呼べないのでは //html要素またはeventの内容に含ませられればリダイレクトできる筈 /*var element = document.getElementById('para');//test var hoge = "<?php echo $hashcode; ?>"; window.alert(element.textContent);*/ location = 'https://apiv4test.itage.jp/thanks/'; /*var inputs = event.detail.inputs; for ( var i = 0; i < inputs.length; i++ ) { if ( 'your-lastname' == inputs[i].name ) { var sei = inputs[i].value; } if ( 'your-firstname' == inputs[i].name ) { var mei = inputs[i].value; } if ( 'hashcode' == inputs[i].name ) { var email = inputs[i].value; } } console.log(event);*/ //location = 'https://google.com?c='+sei; }, false); </script> <?php }
- トピック「テーマのfunctions.phpに書いた関数内変数の受け渡し」には新たに返信することはできません。