ロゴ
  • about
  • Contact
    カテゴリ

    WordPress

    時間undefined/undefined/undefined

    フォームからお問い合わせがあった際に、自動投稿する方法

    <p>wordpressのサイトで、お見積りのお問い合わせがあった場合、その内容を自動投稿する方法を調べました。<br><br>参考はこちら<br><a href="https://www.will3in.co.jp/frontend-blog/article/create-post-and-reflect-acf-group-field-from-contactform7/" target="_blank" rel="noopener noreferrer">https://www.will3in.co.jp/frontend-blog/article/create-post-and-reflect-acf-group-field-from-contactform7/</a><br></p><h2 id="h00b8af9eb7">仕様</h2><ul><li>wordpress</li><li>contactform7(フォーム2種類設置)</li><li>acfでカスタムフィールド設定</li><li>カスタム投稿タイプ</li><li>カスタムタクソノミ</li></ul><p><br></p><pre><code>// フォーム送信完了後実行 add_action('wpcf7_mail_sent', 'wpcf7_insert_post');</code></pre><p>cf7にwpcf7<em>mail</em>sentというアクションフックがあるのでこれを利用します。<br>これで、フォームの送信が完了した後に、wpcf7_insert_post関数を実行できます。<br><br>wpcf7<em>insert</em>post関数を作成します。</p><pre><code>function wpcf7_insert_post($contact_form){ &nbsp;&nbsp; &nbsp; // 送信元のform id取得 &nbsp; $form_id = $contact_form-&gt;id(); // 特定のフォームからのみ実行 &nbsp; if($form_id == "2189"){ &nbsp; &nbsp; // おまじない &nbsp; &nbsp; $submission = WPCF7_Submission::get_instance(); &nbsp; &nbsp; if($submission){ &nbsp; &nbsp; &nbsp; // フォーム内容取得 &nbsp; &nbsp; &nbsp; $form_data = $submission-&gt;get_posted_data(); &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; // 投稿内容セット &nbsp; &nbsp; &nbsp; $new_post = array( &nbsp; &nbsp; &nbsp; &nbsp; "post_type"=&gt;'item', //カスタム投稿タイプ指定 &nbsp; &nbsp; &nbsp; &nbsp; 'post_title' =&gt; $form_data['your-type'], //記事タイトル設定 &nbsp; &nbsp; &nbsp; &nbsp; 'post_content' =&gt; $form_data['your-textarea'], //記事内容設定 &nbsp; &nbsp; &nbsp; &nbsp; 'post_status' =&gt; 'publish', //記事を公開状態に &nbsp; &nbsp; &nbsp; ); &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; // 新規投稿、投稿ID取得 &nbsp; &nbsp; &nbsp; $post_id = wp_insert_post($new_post); &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; // タームにチェック &nbsp; &nbsp; &nbsp; wp_set_object_terms($post_id, $form_data['your-type'], 'genre'); &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; // カスタムフィールド追加 &nbsp; &nbsp; &nbsp; if(!is_wp_error($post_id)){ &nbsp; &nbsp; &nbsp; &nbsp; add_post_meta($post_id, 'price', $form_data['your-textarea']); &nbsp; &nbsp; &nbsp; &nbsp; add_post_meta($post_id, 'publish', $form_id); &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; } &nbsp; } }</code></pre><p><br>これでお問い合わせがあった際、必要な項目を抜き出して、自動投稿することができます。<br></p>

screew

カテゴリ

  • Hasura
  • WordPress
  • デザイン
  • コーディング
  • About
  • Contact
  • カテゴリ

© All rights reserved