Title: Add Contact Form form using wp_insert_post
Last modified: September 1, 2016

---

# Add Contact Form form using wp_insert_post

 *  Resolved [esemlabel](https://wordpress.org/support/users/esemlabel/)
 * (@esemlabel)
 * [9 years, 11 months ago](https://wordpress.org/support/topic/add-form-using-wp_insert_post/)
 * How can I correctly add Contact Form using php wp_insert_post?
 * While looking in database, posts (forms) with post_type ‘wpcf7_contact_form’ 
   contains post_content with many linebreaks with form’s and response’s translations
   in one place. Additional, the same form settings is in its post_meta.
 * So, is it possible to add forms (needed for ex. on theme activation) with wp_insert_post
   code? How it can be done safe and correctly?
 * Thank you!
 * [https://wordpress.org/plugins/contact-form-7/](https://wordpress.org/plugins/contact-form-7/)

Viewing 5 replies - 1 through 5 (of 5 total)

 *  Plugin Author [Takayuki Miyoshi](https://wordpress.org/support/users/takayukister/)
 * (@takayukister)
 * [9 years, 11 months ago](https://wordpress.org/support/topic/add-form-using-wp_insert_post/#post-7492621)
 * Try this.
 *     ```
       $contact_form = WPCF7_ContactForm::get_template();
       $post_id = $contact_form->save();
       ```
   
 *  Thread Starter [esemlabel](https://wordpress.org/support/users/esemlabel/)
 * (@esemlabel)
 * [9 years, 11 months ago](https://wordpress.org/support/topic/add-form-using-wp_insert_post/#post-7492633)
 * Thank you!
 * But what if plugin is not install during theme activation?
    How it can be done
   through regular wp functions? For ex.:
 *     ```
       $content = 'what it should be? (form and responses?)';
       $args = array(
           'post_type' => 'wpcf7_contact_form',
           'post_status' => 'publish',
           'post_title' => 'some title',
           'post_content' => $content,
       );
       $form_id = wp_insert_post( $args );
       update_post_meta( $form_id, 'what meta should be saved 1?', 'value?' );
       update_post_meta( $form_id, 'what meta should be saved 2?', 'value?' );
       update_post_meta( $form_id, 'what meta should be saved 3?', 'value?' );
       ...
       ```
   
 *  Plugin Author [Takayuki Miyoshi](https://wordpress.org/support/users/takayukister/)
 * (@takayukister)
 * [9 years, 11 months ago](https://wordpress.org/support/topic/add-form-using-wp_insert_post/#post-7492638)
 * If I were you, I wouldn’t create wpcf7_contact_form posts when Contact Form 7
   isn’t active because they are not needed.
 *  Thread Starter [esemlabel](https://wordpress.org/support/users/esemlabel/)
 * (@esemlabel)
 * [9 years, 11 months ago](https://wordpress.org/support/topic/add-form-using-wp_insert_post/#post-7492775)
 * I’m afraid I have to find how to add forms with wp_insert_post only, because 
   cf7 will be installed, but right after theme actication.
    It’s a pity that you
   can not help me.
 *  Thread Starter [esemlabel](https://wordpress.org/support/users/esemlabel/)
 * (@esemlabel)
 * [9 years, 11 months ago](https://wordpress.org/support/topic/add-form-using-wp_insert_post/#post-7492776)
 * Anyway, I got it worked.
    “wp_insert_post” with ‘post_type’, ‘post_status’ and‘
   post_title’ values, and “update_post_meta” with ‘_locale’ (might not needed),‘
   _mail’, ‘_mail_2’, ‘_form’ and ‘_messages’ keys with corresponding values – works
   as expected.

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Add Contact Form form using wp_insert_post’ is closed to new replies.

 * ![](https://ps.w.org/contact-form-7/assets/icon.svg?rev=2339255)
 * [Contact Form 7](https://wordpress.org/plugins/contact-form-7/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/contact-form-7/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/contact-form-7/)
 * [Active Topics](https://wordpress.org/support/plugin/contact-form-7/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/contact-form-7/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/contact-form-7/reviews/)

 * 5 replies
 * 2 participants
 * Last reply from: [esemlabel](https://wordpress.org/support/users/esemlabel/)
 * Last activity: [9 years, 11 months ago](https://wordpress.org/support/topic/add-form-using-wp_insert_post/#post-7492776)
 * Status: resolved