Contact Form7に複数の値を渡す方法


Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /home/webstyle/php-fan.org/public_html/wp-content/plugins/wp-syntax/wp-syntax.php on line 383

Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /home/webstyle/php-fan.org/public_html/wp-content/plugins/wp-syntax/wp-syntax.php on line 383

プラグインのContact Form7に複数の値を渡す方法をご紹介したいと思います。

結局のところは$_GETで渡すだけとシンプルですね。
contact-form-7のテキストフィールドの名前と$_GETの値を合わせます。
例としてココのページ部分に実装しましたので確認できます。プロフィールPHP-SAMPLE-PAGEダウンロードの一番下のあたり、ソーシャルブックマークの下にある→お問い合わせというのがあるのでクリックしてみるとわかります。

テスト的にタイトルと投稿日を取得して渡しております。
$_GET[‘title’]と$_GET[‘sonohoka’]を[text title]と[text sonohoka]に渡しております。


スポンサーリンク

▼functions.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
function my_form_tag_filter($tag){
  if ( ! is_array( $tag ) )
  return $tag;
 
  if(isset($_GET['title'])){
    $name = $tag['name'];
    if($name == 'title')
      $tag['values'] = (array) $_GET['title'];
  }
 
  if(isset($_GET['sonohoka'])){
    $name = $tag['name'];
    if($name == 'sonohoka')
      $tag['values'] = (array) $_GET['sonohoka'];
  }
 
  return $tag;
}
add_filter('wpcf7_form_tag', 'my_form_tag_filter', 11);

▼page.php

下記をpage.phpに付け足します。特定のページのみに表示させたい場合はpage-tokutei.phpのようにpage-の後にページファイル名を記述してアップするとできます。

<p><a href="https://php-fan.org/contact/?title=<?php echo urlencode(get_the_title());?>&sonohoka=<?php echo urlencode( the_date('Y年m月d日(D)' ,'','',false));?>">→お問い合わせ</a></p>

IEだと表示されないのでphpのurlencodeで出力させましょう!

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です