Reading and writing post meta (custom fields)

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.

This guide will show you how to get a custom field value into the form, and update that value when saving the form. The example will save and update 3 different custom fields.

Step 1

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

1
2
3456
78
9
10
11
12
13141516
17
function my_quform_load_custom_fields($form)
{
    $form->setValue('iphorm_1_1', get_post_meta(get_the_ID(), 'my_custom_field_1', true));    $form->setValue('iphorm_1_2', get_post_meta(get_the_ID(), 'my_custom_field_2', true));    $form->setValue('iphorm_1_3', get_post_meta(get_the_ID(), 'my_custom_field_3', true));}
add_action('iphorm_pre_display_1', 'my_quform_load_custom_fields'); 
function my_quform_save_custom_fields($form)
{
    $postId = isset($_POST['post_id']) ? $_POST['post_id'] : null;
 
    update_post_meta($postId, 'my_custom_field_1', $form->getValue('iphorm_1_1'));    update_post_meta($postId, 'my_custom_field_2', $form->getValue('iphorm_1_2'));    update_post_meta($postId, 'my_custom_field_3', $form->getValue('iphorm_1_3'));}
add_action('iphorm_post_process_1', 'my_quform_save_custom_fields');
function my_quform_load_custom_fields($form)
{
    $form->setValue('iphorm_1_1', get_post_meta(get_the_ID(), 'my_custom_field_1', true));
    $form->setValue('iphorm_1_2', get_post_meta(get_the_ID(), 'my_custom_field_2', true));
    $form->setValue('iphorm_1_3', get_post_meta(get_the_ID(), 'my_custom_field_3', true));
}
add_action('iphorm_pre_display_1', 'my_quform_load_custom_fields');

function my_quform_save_custom_fields($form)
{
    $postId = isset($_POST['post_id']) ? $_POST['post_id'] : null;

    update_post_meta($postId, 'my_custom_field_1', $form->getValue('iphorm_1_1'));
    update_post_meta($postId, 'my_custom_field_2', $form->getValue('iphorm_1_2'));
    update_post_meta($postId, 'my_custom_field_3', $form->getValue('iphorm_1_3'));
}
add_action('iphorm_post_process_1', 'my_quform_save_custom_fields');
  • On lines 3 and 13 replace iphorm_1_1 with the unique ID of the first field, and replace my_custom_field_1 with the custom field name
  • On lines 4 and 14 replace iphorm_1_2 with the unique ID of the second field, and replace my_custom_field_2 with the custom field name
  • On lines 5 and 15 replace iphorm_1_3 with the unique ID of the third field, and replace my_custom_field_3 with the custom field name
  • On lines 7 and 17 replace the number 1 with the form ID

Step 2 (optional)

By default the form values will be reset when submitting the form, you might want to keep the submitted values in the form. To do that go to Form Builder → Settings → General → Successful submit options, and at Reset form values choose Keep form values.

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