Blog

Contact Form 7でメールの送信内容をカスタムフィールドに保存する

Posted by admin at 17:42 日時 2013/11/11

WordPressの有名なお問い合わせフォーム用プラグインContact Form 7は基本的にメールを送信するだけですが、Flamingoプラグインを併用することで、メールの送信内容をデータベース保存しておくことができます。Flamingoプラグインを使わずに、例えばメールの送信内容をカスタムフィールドに登録して行くという場合の処理を考えてみました。

まず、FlamingoはどうやってContact Form 7の情報を取得しているのでしょうか。その秘密はContact Form 7に同梱されている modules/flamingo.php にありました。下記に引用します。

add_action( 'wpcf7_before_send_mail', 'wpcf7_flamingo_before_send_mail' );    function wpcf7_flamingo_before_send_mail( $contactform ) {  	if ( ! ( class_exists( 'Flamingo_Contact' ) && class_exists( 'Flamingo_Inbound_Message' ) ) )  		return;    	if ( empty( $contactform->posted_data ) || ! empty( $contactform->skip_mail ) )  		return;    	$fields_senseless = $contactform->form_scan_shortcode(  		array( 'type' => array( 'captchar', 'quiz', 'acceptance' ) ) );    	$exclude_names = array();    	foreach ( $fields_senseless as $tag )  		$exclude_names[] = $tag['name'];    	$posted_data = $contactform->posted_data;    	foreach ( $posted_data as $key => $value ) {  		if ( '_' == substr( $key, 0, 1 ) || in_array( $key, $exclude_names ) )  			unset( $posted_data[$key] );  	}    	$email = isset( $posted_data['your-email'] ) ? trim( $posted_data['your-email'] ) : '';  	$name = isset( $posted_data['your-name'] ) ? trim( $posted_data['your-name'] ) : '';  	$subject = isset( $posted_data['your-subject'] ) ? trim( $posted_data['your-subject'] ) : '';    	$meta = array();    	$special_mail_tags = array( 'remote_ip', 'user_agent', 'url', 'date', 'time',  		'post_id', 'post_name', 'post_title', 'post_url', 'post_author', 'post_author_email' );    	foreach ( $special_mail_tags as $smt )  		$meta[$smt] = apply_filters( 'wpcf7_special_mail_tags', '', '_' . $smt, false );    	$akismet = isset( $contactform->akismet ) ? (array) $contactform->akismet : null;    	Flamingo_Contact::add( array(  		'email' => $email,  		'name' => $name ) );    	Flamingo_Inbound_Message::add( array(  		'channel' => 'contact-form-7',  		'subject' => $subject,  		'from' => trim( sprintf( '%s <%s>', $name, $email ) ),  		'from_name' => $name,  		'from_email' => $email,  		'fields' => $posted_data,  		'meta' => $meta,  		'akismet' => $akismet ) );  }

このコードを見ると分かる通り、wpcf7_before_send_mailというアクションフックで、メールの送信前に処理を追加することができるようです。Contact Form 7のプラグイン内で、Flamingoプラグインが有効化されている場合はFlamingoに情報を追加するという処理が書かれています。なので、FlamingoがCF7の情報を取得しているというよりは、CF7からFlamingoに情報を送っていると言うことですね。

wpcf7_before_send_mailアクションはdo_action_ref_arrayで呼ばれていて、引数の$contactformは参照で渡されているため、Contact Form 7のオブジェクトそのものが渡されてきています。そのため、このフックでContact Form 7の挙動を変更する(たとえば、メールを送信しないとか)ことや、送信前にフォームで入力された値を取得することが可能です。

このフックを使って、Contact Form 7で送信されたデータを、フォームが埋め込まれた投稿のカスタムフィールドに追加して行くという処理の実例がこちらです。

  /**   * customize contact form 7   */  function my_wpcf7_before_send_mail( $wpcf7 ) {    	// get post id  	$post_id = apply_filters( 'wpcf7_special_mail_tags', '', '_post_id', false );  	  	// get posted data  	if ( isset($wpcf7->posted_data) && is_array($wpcf7->posted_data) ) {  		foreach ( $wpcf7->posted_data as $key => $value ) {  			if ($value != '')  				add_post_meta($post_id, $key, $value);  		}  	}  	  }  add_action( 'wpcf7_before_send_mail', 'my_wpcf7_before_send_mail' );

投稿IDを取得するのにフィルターを使っていますが、テーマに埋め込むと動かないのでご注意ください。Contact Form 7、さすが柔軟な設計になっていますね!


Share this entry