Contact Form 7は、問合せフォーム以外にも色んな使い道がある。
- 記事の投稿フォームにする
- メールを送信すると同時にhogehogeする
- Contact Form 7を読み込んでいるページにのみhogehogeする
など。
そのために、Contact Form 7に仕込んであるhookを使うと便利。Contact Form 7 (ver 4.3.1)には以下のhookが仕込まれている:
admin/includes/admin-functions.php: do_action( 'wpcf7_save_contact_form', $contact_form ); admin/edit-contact-form.php:<?php do_action( 'wpcf7_admin_notices' ); ?> admin/edit-contact-form.php:<form method="post" action="<?php echo esc_url( add_query_arg( array( 'post' => $post_id ), menu_page_url( 'wpcf7', false ) ) ); ?>" id="wpcf7-admin-form-element"<?php do_action( 'wpcf7_post_edit_form_tag' ); ?>> admin/edit-contact-form.php: do_action( 'wpcf7_admin_footer', $post ); admin/admin.php: do_action( 'wpcf7_admin_init' ); admin/admin.php:<?php do_action( 'wpcf7_admin_notices' ); ?> admin/admin.php:<?php do_action( 'wpcf7_admin_notices' ); ?> admin/admin.php:<?php do_action( 'wpcf7_admin_notices' ); ?> includes/contact-form.php: do_action( 'wpcf7_contact_form', $this ); includes/contact-form.php: do_action( 'wpcf7_submit', $this, $result ); includes/contact-form.php: do_action( 'wpcf7_after_create', $this ); includes/contact-form.php: do_action( 'wpcf7_after_update', $this ); includes/contact-form.php: do_action( 'wpcf7_after_save', $this ); includes/controller.php: do_action( 'wpcf7_enqueue_scripts' ); includes/controller.php: do_action( 'wpcf7_enqueue_styles' ); includes/submission.php: do_action( 'wpcf7_mail_sent', $contact_form ); includes/submission.php: do_action( 'wpcf7_mail_failed', $contact_form ); includes/submission.php: do_action( 'wpcf7_before_send_mail', $contact_form ); settings.php: do_action( 'wpcf7_init' ); settings.php: do_action( 'wpcf7_upgrade', $new_ver, $old_ver );
source: https://wordpress.org/support/topic/list-of-contact-form-7-hooks
例えば、メール送信後にその内容を投稿として保存するには
add_action('wpcf7_mail_sent', 'gloc_wpcf7_insert_post', 10, 1); function gloc_wpcf7_insert_post(){ $submission = WPCF7_Submission::get_instance(); if ( $submission ) { $formdata = $submission->get_posted_data(); $email = $formdata['your-email']; $name = $formdata['your-name']; $new_post = array( 'post_type' => 'post', 'post_title' => $formdata['your-email'] . ' ' . $formdata['your-name'] , 'post_status' => 'draft', 'post_content' => $formdata['your-message'] ); $post_id = wp_insert_post( $new_post ); // metaに入れたり add_post_meta( $post_id, 'some_meta', $formdata['some_meta']); } }
2018年9月21日追記
WordPressのhookを調べるためのサイトを作りました。
例えば、Contact Form 7のhook一覧はこちら
https://www.wp-plugin-api.com/version/contact-form-7/