• Resolved aaronrobb

    (@aaronrobb)


    Thanks for making this plugin! Definitely an upgrade from the basic file uploader.

    I am also using your rename uploaded files function, which works great, but I’m wondering if you know of a way to include the field label in the final file name? I have the field ID working, but including the label would be excellent.
    Possible?

    Thanks!

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author ovann86

    (@ovann86)

    Hey, something like this should do the trick.

    add_filter( 'itsg_gf_ajaxupload_filename', 'my_itsg_gf_ajaxupload_filename', 10, 7 );
    function my_itsg_gf_ajaxupload_filename( $name, $file_path, $size, $type, $error, $index, $content_range ) {
        $form_id = isset( $_POST['form_id'] ) ? $_POST['form_id'] : null; // get the form_id out of the post
        $field_id = isset( $_POST['field_id'] ) ? $_POST['field_id'] : null; // get the field_id out of the post
        $file_extension = pathinfo( $name, PATHINFO_EXTENSION );  // get the file type out of the URL
    	
    	$form_meta = GFFormsModel::get_form_meta( $form_id ); // load the form meta
    	$field = GFFormsModel::get_field( $form_meta, $field_id ); // get the field
    
        $field_label = GFFormsModel::get_label( $field ); // get the field label
    	$field_label = preg_replace( '/[^A-Za-z0-9 ]/', '', $field_label ); // make sure the field label only includes supported characters -- restricting to A-Z 0-9
    	
        $name = $field_label . '_' . $form_id . '_' . $field_id . '.' . $file_extension; // put it together
    	
        return $name; // now return the new name
    }

    No need to worry about duplicate file names being uploaded to the same folder, the plugin will add a number to the file if it already exists.

    • This reply was modified 8 years, 3 months ago by ovann86.
    Thread Starter aaronrobb

    (@aaronrobb)

    Brilliant thank you!
    I was so close myself. Just missed it a bit. ??

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Adding Field Label to File name’ is closed to new replies.