This documentation page is for Quform version 1 and may not be applicable for Quform 2 click here to visit the documentation for Quform 2.
In this guide we will create a standard WordPress post from a form with two fields Title and Content. The functionality can be easily extended to insert Custom post types, by referring to the documentation for wp_insert_post.
Step 1
Get the unique ID of the form, see Finding the form ID.
Step 2
Get the unique element IDs for the both of the fields (the Title and Content elements), see Finding the unique element ID.
Step 3
Add the following code to the wp-content/themes/YOUR_THEME/functions.php file (or create a plugin for it).
1 2 345 6 7 8 9 10 11 12 13 | function my_create_wp_post($form) { $title = $form->getValue('iphorm_3_1'); $content = $form->getValue('iphorm_3_2'); $post = array( 'post_title' => $title, 'post_content' => $content ); wp_insert_post($post); } add_action('iphorm_post_process_3', 'my_create_wp_post', 10, 1); |
function my_create_wp_post($form) { $title = $form->getValue('iphorm_3_1'); $content = $form->getValue('iphorm_3_2'); $post = array( 'post_title' => $title, 'post_content' => $content ); wp_insert_post($post); } add_action('iphorm_post_process_3', 'my_create_wp_post', 10, 1);
Modify the highlighted lines to suit the form setup:
- On line 3, change
iphorm_3_1
to the unique ID of the title element, from Step 2 - On line 4, change
iphorm_3_2
to the unique ID of the content element, from Step 2 - On line 13, change the number
3
to the form ID, from Step 1
Creating a page
To create a page, simply change the $post
array to have a post_type
line with the value page
.
1
2
3
45
| $post = array( 'post_title' => $title, 'post_content' => $content, 'post_type' => 'page'); |
$post = array( 'post_title' => $title, 'post_content' => $content, 'post_type' => 'page' );
Custom post types
To insert a custom post type, simply change the $post
array to have another post_type
line with the name of the post type.
1
2
3
45
| $post = array( 'post_title' => $title, 'post_content' => $content, 'post_type' => 'portfolio'); |
$post = array( 'post_title' => $title, 'post_content' => $content, 'post_type' => 'portfolio' );
Adding custom post content
If you want to add form values along with custom HTML, we can modify the above guide to allow this.
Step 1
Create a file named form-post.php and place it into your theme root directory wp-content/themes/YOUR_THEME/form-post.php
Inside this file add the required post content in text and HTML, you can use the PHP code shown in the example below to get the form values for particular elements, just replace iphorm_3_1
with the element unique ID.
<?php if (!defined('ABSPATH')) exit; // Prevent direct script access ?> <h2>Submitted form data</h2> <p>Submitted field 1: <?php echo $form->getValueHtml('iphorm_3_1'); ?></p> <p>Submitted field 2: <?php echo $form->getValueHtml('iphorm_3_2'); ?></p> <p>Submitted field 3: <?php echo $form->getValueHtml('iphorm_3_3'); ?></p> <p>Submitted field 4: <?php echo $form->getValueHtml('iphorm_3_4'); ?></p> <p>Submitted field 5: <?php echo $form->getValueHtml('iphorm_3_5'); ?></p>
Step 2
Add the following code to the wp-content/themes/YOUR_THEME/functions.php file (or create a plugin for it).
1 2 34 5 6 7 8 9 10 11 12 13 14 15 | function my_create_wp_post($form) { $title = $form->getValue('iphorm_3_1'); ob_start(); include dirname(__FILE__) . '/form-post.php'; $content = ob_get_clean(); $post = array( 'post_title' => $title, 'post_content' => $content ); wp_insert_post($post); } add_action('iphorm_post_process_3', 'my_create_wp_post', 10, 1); |
function my_create_wp_post($form) { $title = $form->getValue('iphorm_3_1'); ob_start(); include dirname(__FILE__) . '/form-post.php'; $content = ob_get_clean(); $post = array( 'post_title' => $title, 'post_content' => $content ); wp_insert_post($post); } add_action('iphorm_post_process_3', 'my_create_wp_post', 10, 1);
Modify the code to suit the form setup:
- On line 3, change
iphorm_3_1
to the unique ID of the title element - On line 15, change the number
3
to the form ID - Customize the form-post.php file with your custom HTML and form values
Adding the created post to a category
To add the post to a category, the code is slightly different.
Step 1
Get the unique ID of the form, see Finding the form ID.
Step 2
Get the unique element IDs for the both of the fields (the Title and Content elements), see Finding the unique element ID.
Step 3
Add the following code to the wp-content/themes/YOUR_THEME/functions.php file (or create a plugin for it).
1 2 345 6 7 8 9 10 11 1213 14 | function my_create_wp_post($form) { $title = $form->getValue('iphorm_3_1'); $content = $form->getValue('iphorm_3_2'); $post = array( 'post_title' => $title, 'post_content' => $content ); $postId = wp_insert_post($post); wp_set_object_terms($postId, 'my_category_slug', 'category');} add_action('iphorm_post_process_3', 'my_create_wp_post', 10, 1); |
function my_create_wp_post($form) { $title = $form->getValue('iphorm_3_1'); $content = $form->getValue('iphorm_3_2'); $post = array( 'post_title' => $title, 'post_content' => $content ); $postId = wp_insert_post($post); wp_set_object_terms($postId, 'my_category_slug', 'category'); } add_action('iphorm_post_process_3', 'my_create_wp_post', 10, 1);
Modify the highlighted lines to suit the form setup:
- On line 3, change
iphorm_3_1
to the unique ID of the title element, from Step 2 - On line 4, change
iphorm_3_2
to the unique ID of the content element, from Step 2 - On line 12,
my_category_slug
to the slug of the category you want to add - On line 14, change the number
3
to the form ID, from Step 1
Adding a featured image from a File Upload element
To add a featured image from a File Upload element, you’ll need to add additional code shown in the example below.
Step 1
Get the unique ID of the form, see Finding the form ID.
Step 2
Get the unique element IDs for the all of the fields (the Title, Content and File Upload elements), see Finding the unique element ID.
Step 3
Add the following code to the wp-content/themes/YOUR_THEME/functions.php file (or create a plugin for it).
1 2 345 6 7 8 9 10 11 12 13 1415 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | function my_create_wp_post($form) { $title = $form->getValue('iphorm_3_1'); $content = $form->getValue('iphorm_3_2'); $post = array( 'post_title' => $title, 'post_content' => $content ); $postId = wp_insert_post($post); // Add attachments $file = $form->getValue('iphorm_3_3'); if (isset($file[0])) { $file = $file[0]; $filename = $file['text']; $path = $file['fullPath']; $wp_filetype = wp_check_filetype($filename, null ); $attachment = array( 'guid' => $file['url'], 'post_mime_type' => $wp_filetype['type'], 'post_title' => preg_replace('/\.[^.]+$/', '', $filename), 'post_content' => '', 'post_status' => 'inherit' ); $attach_id = wp_insert_attachment($attachment, $path, $postId); require_once ABSPATH . 'wp-admin/includes/image.php'; $attach_data = wp_generate_attachment_metadata($attach_id, $path); wp_update_attachment_metadata($attach_id, $attach_data); set_post_thumbnail($postId, $attach_id); } } add_action('iphorm_post_process_3', 'my_create_wp_post', 10, 1); |
function my_create_wp_post($form) { $title = $form->getValue('iphorm_3_1'); $content = $form->getValue('iphorm_3_2'); $post = array( 'post_title' => $title, 'post_content' => $content ); $postId = wp_insert_post($post); // Add attachments $file = $form->getValue('iphorm_3_3'); if (isset($file[0])) { $file = $file[0]; $filename = $file['text']; $path = $file['fullPath']; $wp_filetype = wp_check_filetype($filename, null ); $attachment = array( 'guid' => $file['url'], 'post_mime_type' => $wp_filetype['type'], 'post_title' => preg_replace('/\.[^.]+$/', '', $filename), 'post_content' => '', 'post_status' => 'inherit' ); $attach_id = wp_insert_attachment($attachment, $path, $postId); require_once ABSPATH . 'wp-admin/includes/image.php'; $attach_data = wp_generate_attachment_metadata($attach_id, $path); wp_update_attachment_metadata($attach_id, $attach_data); set_post_thumbnail($postId, $attach_id); } } add_action('iphorm_post_process_3', 'my_create_wp_post', 10, 1);
Modify the highlighted lines to suit the form setup:
- On line 3, change
iphorm_3_1
to the unique ID of the title element, from Step 2 - On line 4, change
iphorm_3_2
to the unique ID of the content element, from Step 2 - On line 14,
iphorm_3_3
to the unique ID of the File Upload element, from Step 2 - On line 40, change the number
3
to the form ID, from Step 1