Adding file uploads to the WordPress media library

Go to the File Upload element settings.

Go to the File Upload element settings

Then go to the Data tab.

Go to the Data tab

Then enable the option Add to media library.

Enable the Add to media library option

Show legacy instructions

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

12
3
45
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
add_filter('quform_post_process_1', function (array $result, Quform_Form $form) {    require_once ABSPATH . 'wp-admin/includes/image.php';
    require_once ABSPATH . 'wp-admin/includes/media.php';
    $files = $form->getValue('quform_1_3'); 
    foreach ($files as $file) {
        $type = wp_check_filetype($file['name'], null);
 
        $attachment = array(
            'post_title' => $file['name'],
            'post_content' => '',
            'post_mime_type' => $type['type'],
            'guid' => $file['url']
        );
 
        $attachId = wp_insert_attachment($attachment, $file['path']);
        wp_update_attachment_metadata($attachId, wp_generate_attachment_metadata($attachId, $file['path']));
    }
 
    return $result;
}, 10, 2);
add_filter('quform_post_process_1', function (array $result, Quform_Form $form) {
    require_once ABSPATH . 'wp-admin/includes/image.php';
    require_once ABSPATH . 'wp-admin/includes/media.php';
    $files = $form->getValue('quform_1_3');

    foreach ($files as $file) {
        $type = wp_check_filetype($file['name'], null);

        $attachment = array(
            'post_title' => $file['name'],
            'post_content' => '',
            'post_mime_type' => $type['type'],
            'guid' => $file['url']
        );

        $attachId = wp_insert_attachment($attachment, $file['path']);
        wp_update_attachment_metadata($attachId, wp_generate_attachment_metadata($attachId, $file['path']));
    }

    return $result;
}, 10, 2);
  • On line 1, replace the number 1 with the form ID
  • On line 4, replace 1_3 with the File Upload element unique ID
1
2
3
45
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2223
function my_save_uploads_to_media_library(array $result, Quform_Form $form)
{
    require_once ABSPATH . 'wp-admin/includes/image.php';
    require_once ABSPATH . 'wp-admin/includes/media.php';    $files = $form->getValue('quform_1_3');
 
    foreach ($files as $file) {
        $type = wp_check_filetype($file['name'], null);
 
        $attachment = array(
            'post_title' => $file['name'],
            'post_content' => '',
            'post_mime_type' => $type['type'],
            'guid' => $file['url']
        );
 
        $attachId = wp_insert_attachment($attachment, $file['path']);
        wp_update_attachment_metadata($attachId, wp_generate_attachment_metadata($attachId, $file['path']));
    }
 
    return $result;
}add_filter('quform_post_process_1', 'my_save_uploads_to_media_library', 10, 2);
function my_save_uploads_to_media_library(array $result, Quform_Form $form)
{
    require_once ABSPATH . 'wp-admin/includes/image.php';
    require_once ABSPATH . 'wp-admin/includes/media.php';
    $files = $form->getValue('quform_1_3');

    foreach ($files as $file) {
        $type = wp_check_filetype($file['name'], null);

        $attachment = array(
            'post_title' => $file['name'],
            'post_content' => '',
            'post_mime_type' => $type['type'],
            'guid' => $file['url']
        );

        $attachId = wp_insert_attachment($attachment, $file['path']);
        wp_update_attachment_metadata($attachId, wp_generate_attachment_metadata($attachId, $file['path']));
    }

    return $result;
}
add_filter('quform_post_process_1', 'my_save_uploads_to_media_library', 10, 2);
  • On line 4, replace 1_3 with the File Upload element unique ID
  • On line 22, replace the number 1 with the form ID
Be inspired. © 2024 ThemeCatcher Ltd. 20-22 Wenlock Road, London, England, N1 7GU | Company No. 08120384 | Built with React | Privacy Policy