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 the wp_insert_post WordPress function.

Creating a post

Add the following code to the theme functions.php file (or create a plugin for it).

1234
5
6
7
8
9
10
11
12
13
14
15
add_filter('quform_post_process_1', function (array $result, Quform_Form $form) {    $title = $form->getValue('quform_1_3');    $content = $form->getValue('quform_1_4'); 
    $post = array(
        'post_title' => $title,
        'post_content' => $content,
        'post_type' => 'post',
        'post_status' => 'publish'
    );
 
    wp_insert_post($post);
 
    return $result;
}, 10, 2);
add_filter('quform_post_process_1', function (array $result, Quform_Form $form) {
    $title = $form->getValue('quform_1_3');
    $content = $form->getValue('quform_1_4');

    $post = array(
        'post_title' => $title,
        'post_content' => $content,
        'post_type' => 'post',
        'post_status' => 'publish'
    );

    wp_insert_post($post);

    return $result;
}, 10, 2);
  • On line 1, change the number 1 to the form ID
  • On line 2, change 1_3 to the unique ID of the title Text element
  • On line 3, change 1_4 to the unique ID of the content Textarea element
1
2
345
6
7
8
9
10
11
12
13
14
15
16
17
function my_post_process(array $result, Quform_Form $form)
{
    $title = $form->getValue('quform_1_3');    $content = $form->getValue('quform_1_4'); 
    $post = array(
        'post_title' => $title,
        'post_content' => $content,
        'post_type' => 'post',
        'post_status' => 'publish'
    );
 
    wp_insert_post($post);
 
    return $result;
}
add_filter('quform_post_process_1', 'my_post_process', 10, 2);
function my_post_process(array $result, Quform_Form $form)
{
    $title = $form->getValue('quform_1_3');
    $content = $form->getValue('quform_1_4');

    $post = array(
        'post_title' => $title,
        'post_content' => $content,
        'post_type' => 'post',
        'post_status' => 'publish'
    );

    wp_insert_post($post);

    return $result;
}
add_filter('quform_post_process_1', 'my_post_process', 10, 2);
  • On line 3, change 1_3 to the unique ID of the title Text element
  • On line 4, change 1_4 to the unique ID of the content Textarea element
  • On line 17, change the number 1 to the form ID

Creating a page

To create a page, simply change the post_type to page.

1
2
3
45
6
$post = array(
    'post_title' => $title,
    'post_content' => $content,
    'post_type' => 'page',    'post_status' => 'publish'
);
$post = array(
    'post_title' => $title,
    'post_content' => $content,
    'post_type' => 'page',
    'post_status' => 'publish'
);

Custom post types

To insert a custom post type, simply change the post_type with the custom post type.

1
2
3
45
6
$post = array(
    'post_title' => $title,
    'post_content' => $content,
    'post_type' => 'portfolio',    'post_status' => 'publish'
);
$post = array(
    'post_title' => $title,
    'post_content' => $content,
    'post_type' => 'portfolio',
    'post_status' => 'publish'
);

Creating a draft

To create a draft instead of a published post, simply change the post_status to draft.

1
2
3
4
56
$post = array(
    'post_title' => $title,
    'post_content' => $content,
    'post_type' => 'portfolio',
    'post_status' => 'draft');
$post = array(
    'post_title' => $title,
    'post_content' => $content,
    'post_type' => 'portfolio',
    'post_status' => 'draft'
);

Customizing the post content

To add a mixture of HTML and form values we will create a separate file for the HTML and include it when creating the post.

Step 1

Create a plugin for this custom code.

Step 2

Create a file named form-post.php and place it into the plugin directory at wp-content/plugins/quform-custom-code/form-post.php

Inside this file add the 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 1_3 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('quform_1_3'); ?></p>
<p>Submitted field 2: <?php echo $form->getValueHtml('quform_1_4'); ?></p>
<p>Submitted field 3: <?php echo $form->getValueHtml('quform_1_5'); ?></p>
<p>Submitted field 4: <?php echo $form->getValueHtml('quform_1_6'); ?></p>
<p>Submitted field 5: <?php echo $form->getValueHtml('quform_1_7'); ?></p>

Step 3

Add the following code to the quform-custom-code.php file created in Step 1.

123
4
5
6
7
8
9
10
11
12
13
14
15
16
17
add_filter('quform_post_process_1', function (array $result, Quform_Form $form) {    $title = $form->getValue('quform_1_3');    ob_start();
    include __DIR__ . '/form-post.php';
    $content = ob_get_clean();
 
    $post = array(
        'post_title' => $title,
        'post_content' => $content,
        'post_type' => 'post',
        'post_status' => 'publish'
    );
 
    wp_insert_post($post);
 
    return $result;
}, 10, 2);
add_filter('quform_post_process_1', function (array $result, Quform_Form $form) {
    $title = $form->getValue('quform_1_3');
    ob_start();
    include __DIR__ . '/form-post.php';
    $content = ob_get_clean();

    $post = array(
        'post_title' => $title,
        'post_content' => $content,
        'post_type' => 'post',
        'post_status' => 'publish'
    );

    wp_insert_post($post);

    return $result;
}, 10, 2);
  • On line 1, change the number 1 to the form ID
  • On line 2, change 1_3 to the unique ID of the title Text element
  • Customize the form-post.php file with the custom HTML and form values
1
2
34
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
function my_create_wp_post(array $result, Quform_Form $form)
{
    $title = $form->getValue('quform_1_3');    ob_start();
    include dirname(__FILE__) . '/form-post.php';
    $content = ob_get_clean();
 
    $post = array(
        'post_title' => $title,
        'post_content' => $content,
        'post_type' => 'post',
        'post_status' => 'publish'
    );
 
    wp_insert_post($post);
 
    return $result;
}
add_filter('quform_post_process_1', 'my_create_wp_post', 10, 2);
function my_create_wp_post(array $result, Quform_Form $form)
{
    $title = $form->getValue('quform_1_3');
    ob_start();
    include dirname(__FILE__) . '/form-post.php';
    $content = ob_get_clean();

    $post = array(
        'post_title' => $title,
        'post_content' => $content,
        'post_type' => 'post',
        'post_status' => 'publish'
    );

    wp_insert_post($post);

    return $result;
}
add_filter('quform_post_process_1', 'my_create_wp_post', 10, 2);
  • On line 3, change 1_3 to the unique ID of the title Text element
  • On line 19, change the number 1 to the form ID
  • Customize the form-post.php file with the custom HTML and form values

Saving data to custom fields (postmeta)

To save data to custom fields, add the following code to the theme functions.php file (or create a plugin for it).

1234
5
6
7
8
9
10
11
12
13
14151617
18
19
add_filter('quform_post_process_1', function (array $result, Quform_Form $form) {    $title = $form->getValue('quform_1_3');    $content = $form->getValue('quform_1_4'); 
    $post = array(
        'post_title' => $title,
        'post_content' => $content,
        'post_type' => 'post',
        'post_status' => 'publish'
    );
 
    $postId = wp_insert_post($post);
    
    add_post_meta($postId, 'my_custom_field_key_1', $form->getValue('quform_1_5'));    add_post_meta($postId, 'my_custom_field_key_2', $form->getValue('quform_1_6'));    add_post_meta($postId, 'my_custom_field_key_3', $form->getValue('quform_1_7')); 
    return $result;
}, 10, 2);
add_filter('quform_post_process_1', function (array $result, Quform_Form $form) {
    $title = $form->getValue('quform_1_3');
    $content = $form->getValue('quform_1_4');

    $post = array(
        'post_title' => $title,
        'post_content' => $content,
        'post_type' => 'post',
        'post_status' => 'publish'
    );

    $postId = wp_insert_post($post);
    
    add_post_meta($postId, 'my_custom_field_key_1', $form->getValue('quform_1_5'));
    add_post_meta($postId, 'my_custom_field_key_2', $form->getValue('quform_1_6'));
    add_post_meta($postId, 'my_custom_field_key_3', $form->getValue('quform_1_7'));

    return $result;
}, 10, 2);
  • On line 1, change the number 1 to the form ID
  • On line 2, change 1_3 to the unique ID of the title Text element
  • On line 3, change 1_4 to the unique ID of the content Textarea element
  • On lines 14, 15 and 16, change my_custom_field_key_1, my_custom_field_key_2 and my_custom_field_key_3to the custom field meta keys you want to add the data to, and change 1_5, 1_6 and 1_7 to the unique ID of the elements you want to take the data from
1
2
345
6
7
8
9
10
11
12
13
14
15161718
19
20
21
function my_create_wp_post(array $result, Quform_Form $form)
{
    $title = $form->getValue('quform_1_3');    $content = $form->getValue('quform_1_4'); 
    $post = array(
        'post_title' => $title,
        'post_content' => $content,
        'post_type' => 'post',
        'post_status' => 'publish'
    );
 
    $postId = wp_insert_post($post);
 
    add_post_meta($postId, 'my_custom_field_key_1', $form->getValue('quform_1_5'));    add_post_meta($postId, 'my_custom_field_key_2', $form->getValue('quform_1_6'));    add_post_meta($postId, 'my_custom_field_key_3', $form->getValue('quform_1_7')); 
    return $result;
}
add_filter('quform_post_process_1', 'my_create_wp_post', 10, 2);
function my_create_wp_post(array $result, Quform_Form $form)
{
    $title = $form->getValue('quform_1_3');
    $content = $form->getValue('quform_1_4');

    $post = array(
        'post_title' => $title,
        'post_content' => $content,
        'post_type' => 'post',
        'post_status' => 'publish'
    );

    $postId = wp_insert_post($post);

    add_post_meta($postId, 'my_custom_field_key_1', $form->getValue('quform_1_5'));
    add_post_meta($postId, 'my_custom_field_key_2', $form->getValue('quform_1_6'));
    add_post_meta($postId, 'my_custom_field_key_3', $form->getValue('quform_1_7'));

    return $result;
}
add_filter('quform_post_process_1', 'my_create_wp_post', 10, 2);
  • On line 3, change 1_3 to the unique ID of the title Text element
  • On line 4, change 1_4 to the unique ID of the content Textarea element
  • On lines 15, 16 and 17, change my_custom_field_key_1, my_custom_field_key_2 and my_custom_field_key_3to the custom field meta keys you want to add the data to, and change 1_5, 1_6 and 1_7 to the unique ID of the elements you want to take the data from
  • On line 21, change the number 1 to the form ID

Adding the created post to a category

To add the post to a category, add the following code to the theme functions.php file (or create a plugin for it).

1234
5
6
7
8
9
10
11
12
1314
15
16
add_filter('quform_post_process_1', function (array $result, Quform_Form $form) {    $title = $form->getValue('quform_1_3');    $content = $form->getValue('quform_1_4'); 
    $post = array(
        'post_title' => $title,
        'post_content' => $content,
        'post_type' => 'post',
        'post_status' => 'publish'
    );
 
    $postId = wp_insert_post($post);
    wp_set_object_terms($postId, 'my_category_slug', 'category'); 
    return $result;
}, 10, 2);
add_filter('quform_post_process_1', function (array $result, Quform_Form $form) {
    $title = $form->getValue('quform_1_3');
    $content = $form->getValue('quform_1_4');

    $post = array(
        'post_title' => $title,
        'post_content' => $content,
        'post_type' => 'post',
        'post_status' => 'publish'
    );

    $postId = wp_insert_post($post);
    wp_set_object_terms($postId, 'my_category_slug', 'category');

    return $result;
}, 10, 2);
  • On line 1, change the number 1 to the form ID
  • On line 2, change 1_3 to the unique ID of the title Text element
  • On line 3, change 1_4 to the unique ID of the content Textarea element
  • On line 13, change my_category_slug to the slug of the category you want to add
1
2
345
6
7
8
9
10
11
12
13
1415
16
17
18
function my_create_wp_post(array $result, Quform_Form $form)
{
    $title = $form->getValue('quform_1_3');    $content = $form->getValue('quform_1_4'); 
    $post = array(
        'post_title' => $title,
        'post_content' => $content,
        'post_type' => 'post',
        'post_status' => 'publish'
    );
 
    $postId = wp_insert_post($post);
    wp_set_object_terms($postId, 'my_category_slug', 'category'); 
    return $result;
}
add_filter('quform_post_process_1', 'my_create_wp_post', 10, 2);
function my_create_wp_post(array $result, Quform_Form $form)
{
    $title = $form->getValue('quform_1_3');
    $content = $form->getValue('quform_1_4');

    $post = array(
        'post_title' => $title,
        'post_content' => $content,
        'post_type' => 'post',
        'post_status' => 'publish'
    );

    $postId = wp_insert_post($post);
    wp_set_object_terms($postId, 'my_category_slug', 'category');

    return $result;
}
add_filter('quform_post_process_1', 'my_create_wp_post', 10, 2);
  • On line 3, change 1_3 to the unique ID of the title Text element
  • On line 4, change 1_4 to the unique ID of the content Textarea element
  • On line 14, change my_category_slug to the slug of the category you want to add
  • On line 18, change the number 1 to the form ID

To add a featured image from a File Upload element, you’ll need to add additional code shown in the example below. Add the following code to the theme functions.php file (or create a plugin for it).

1234
5
6
7
8
9
10
11
12
13
14
1516
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
add_filter('quform_post_process_1', function (array $result, Quform_Form $form) {    $title = $form->getValue('quform_1_3');    $content = $form->getValue('quform_1_4'); 
    $post = array(
        'post_title' => $title,
        'post_content' => $content,
        'post_type' => 'post',
        'post_status' => 'publish'
    );
 
    $postId = wp_insert_post($post);
 
    // Add attachment
    $file = $form->getValue('quform_1_5'); 
    if (isset($file[0])) {
        $file = $file[0];
        $filename = $file['name'];
        $path = $file['path'];
 
        $wpFiletype = wp_check_filetype($filename);
 
        $attachment = array(
            'guid' => $file['url'],
            'post_mime_type' => $wpFiletype['type'],
            'post_title' => preg_replace('/\.[^.]+$/', '', $filename),
            'post_content' => '',
            'post_status' => 'inherit'
        );
 
        $attachId = wp_insert_attachment($attachment, $path, $postId);
 
        require_once ABSPATH . 'wp-admin/includes/image.php';
        $attachData = wp_generate_attachment_metadata($attachId, $path);
        wp_update_attachment_metadata($attachId, $attachData);
 
        set_post_thumbnail($postId, $attachId);
    }
 
    return $result;
}, 10, 2);
add_filter('quform_post_process_1', function (array $result, Quform_Form $form) {
    $title = $form->getValue('quform_1_3');
    $content = $form->getValue('quform_1_4');

    $post = array(
        'post_title' => $title,
        'post_content' => $content,
        'post_type' => 'post',
        'post_status' => 'publish'
    );

    $postId = wp_insert_post($post);

    // Add attachment
    $file = $form->getValue('quform_1_5');

    if (isset($file[0])) {
        $file = $file[0];
        $filename = $file['name'];
        $path = $file['path'];

        $wpFiletype = wp_check_filetype($filename);

        $attachment = array(
            'guid' => $file['url'],
            'post_mime_type' => $wpFiletype['type'],
            'post_title' => preg_replace('/\.[^.]+$/', '', $filename),
            'post_content' => '',
            'post_status' => 'inherit'
        );

        $attachId = wp_insert_attachment($attachment, $path, $postId);

        require_once ABSPATH . 'wp-admin/includes/image.php';
        $attachData = wp_generate_attachment_metadata($attachId, $path);
        wp_update_attachment_metadata($attachId, $attachData);

        set_post_thumbnail($postId, $attachId);
    }

    return $result;
}, 10, 2);
  • On line 1, change the number 1 to the form ID
  • On line 2, change 1_3 to the unique ID of the title Text element
  • On line 3, change 1_4 to the unique ID of the content Textarea element
  • On line 15, 1_5 to the unique ID of the File Upload element
1
2
345
6
7
8
9
10
11
12
13
14
15
1617
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
function my_create_wp_post(array $result, Quform_Form $form)
{
    $title = $form->getValue('quform_1_3');    $content = $form->getValue('quform_1_4'); 
    $post = array(
        'post_title' => $title,
        'post_content' => $content,
        'post_type' => 'post',
        'post_status' => 'publish'
    );
 
    $postId = wp_insert_post($post);
 
    // Add attachment
    $file = $form->getValue('quform_1_5'); 
    if (isset($file[0])) {
        $file = $file[0];
        $filename = $file['name'];
        $path = $file['path'];
 
        $wpFiletype = wp_check_filetype($filename);
 
        $attachment = array(
            'guid' => $file['url'],
            'post_mime_type' => $wpFiletype['type'],
            'post_title' => preg_replace('/\.[^.]+$/', '', $filename),
            'post_content' => '',
            'post_status' => 'inherit'
        );
 
        $attachId = wp_insert_attachment($attachment, $path, $postId);
 
        require_once ABSPATH . 'wp-admin/includes/image.php';
        $attachData = wp_generate_attachment_metadata($attachId, $path);
        wp_update_attachment_metadata($attachId, $attachData);
 
        set_post_thumbnail($postId, $attachId);
    }
 
    return $result;
}
add_filter('quform_post_process_1', 'my_create_wp_post', 10, 2);
function my_create_wp_post(array $result, Quform_Form $form)
{
    $title = $form->getValue('quform_1_3');
    $content = $form->getValue('quform_1_4');

    $post = array(
        'post_title' => $title,
        'post_content' => $content,
        'post_type' => 'post',
        'post_status' => 'publish'
    );

    $postId = wp_insert_post($post);

    // Add attachment
    $file = $form->getValue('quform_1_5');

    if (isset($file[0])) {
        $file = $file[0];
        $filename = $file['name'];
        $path = $file['path'];

        $wpFiletype = wp_check_filetype($filename);

        $attachment = array(
            'guid' => $file['url'],
            'post_mime_type' => $wpFiletype['type'],
            'post_title' => preg_replace('/\.[^.]+$/', '', $filename),
            'post_content' => '',
            'post_status' => 'inherit'
        );

        $attachId = wp_insert_attachment($attachment, $path, $postId);

        require_once ABSPATH . 'wp-admin/includes/image.php';
        $attachData = wp_generate_attachment_metadata($attachId, $path);
        wp_update_attachment_metadata($attachId, $attachData);

        set_post_thumbnail($postId, $attachId);
    }

    return $result;
}
add_filter('quform_post_process_1', 'my_create_wp_post', 10, 2);
  • On line 3, change 1_3 to the unique ID of the title Text element
  • On line 4, change 1_4 to the unique ID of the content Textarea element
  • On line 16, 1_5 to the unique ID of the File Upload element
  • On line 44, change the number 1 to the form ID

See also

Be inspired. © 2024 ThemeCatcher Ltd. 20-22 Wenlock Road, London, England, N1 7GU | Company No. 08120384 | Built with React | Privacy Policy