Count uploaded images in field [RESOLVED]

Hi!

I would like to have uploaded fields within a form and counted the number of uploaded images into a single field.

Found this code but this count all upload filed in the form…

add_action( 'gform_pre_render', 'set_number_of_uploaded_images' );
 
// Set the uploaded images count field and make it readonly
function set_number_of_uploaded_images( $form ) {
    // only run hook for form_id = 1
    if ( $form['id'] != 1 ) {
       return $form;
    }
    // make any input field with the class gf_readonly to be read-only 
    ?>
    <script type="text/javascript">
    jQuery(document).ready(function(){
        jQuery("li.gf_readonly input").attr("readonly","readonly");
    });
    </script>
    <script type="text/javascript">
 
        gwCountFiles = function (fieldID, formID) {
 
            function getAllFiles(formID) {
                var selector = '#gform_uploaded_files_' + formID,
                    $uploadedFiles = jQuery(selector), files;
                files = $uploadedFiles.val();
                files = '' === files ? {} : jQuery.parseJSON(files);
                return files;
            }
 
            function getFiles(fieldID, formID) {
                var allFiles = getAllFiles(formID);
                var inputName = getInputName(fieldID);
 
                if (typeof allFiles[inputName] == 'undefined')
                    allFiles[inputName] = [];
                return allFiles[inputName];
            }
 
            function getInputName(fieldID) {
                return "input_" + fieldID;
            }
 
            var files = getFiles(fieldID, formID);
 
            return files.length;
 
        };
 
        gform.addFilter('gform_file_upload_markup', function (html, file, up, strings, imagesUrl) {
            var formId = up.settings.multipart_params.form_id,
                fieldId = up.settings.multipart_params.field_id,
                targetId = 7; // set this to the field_id of your uploaded files count field
 
            html = '<strong>' + file.name + "</strong> <img class='gform_delete' "
            + "src='" + imagesUrl + "/delete.png' "
            + "onclick='gformDeleteUploadedFile(" + formId + "," + fieldId + ", this);countThemFiles(" + formId + ", " + fieldId + ", 0, " + targetId + ");' "
            + "alt='" + strings.delete_file + "' title='" + strings.delete_file + "' />";
            countThemFiles(formId, fieldId, 1, targetId);
            return html;
        });
 
        function countThemFiles(formId, fieldId, offset, targetId) {
            var count = gwCountFiles(fieldId, formId);
            jQuery('#input_' + formId + '_' + targetId).val(count + offset).change();
        }
 
    </script>
 
    <?php
    return $form;
}

It is possible?

thanks
Zoltán Oláh

HI,
I have written a plugin for this . If you need let me know.

Hi.

Thank you. I found it, will be buy the plugin.

1 Like