• Resolved esemlabel

    (@esemlabel)


    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/

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Takayuki Miyoshi

    (@takayukister)

    Try this.

    $contact_form = WPCF7_ContactForm::get_template();
    $post_id = $contact_form->save();

    Thread Starter esemlabel

    (@esemlabel)

    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

    (@takayukister)

    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

    (@esemlabel)

    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

    (@esemlabel)

    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.