Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Gravity Forms - Upload File Attachement With Title WordPress

  • REFUNDED

Hello

I would like your help with gravity forms and wordpress.

1 - We currently have a form that is creating a new post of a custom post type.
2 - Would like the option to upload multiple files (various formats, image, doc etc) as part of this submission
3 - Files are to be attached to the post and visible in the media manager, NOT into a custom field. Attachments should be accessable to shortcode plugins like https://wordpress.org/plugins/list-attachments-shortcode/
4 - Each file needs to have a title (the media manager field) entered as part of the upload

Inputs
- File selected from explorer
- Title entered in text field

Results
- File attached to post
- Title field populated in media manager

Notes
- Up to 10 files could be uploaded, so ideally a repeater would be used
- I can provide access to a staging environment, please message me

Answers (1)

2015-02-28

Romel Apuya answers:

Since version 1.8 GForm is already Supporting multiple File upload
http://bobwp.com/new-features-will-find-gravity-forms-1-8/


Romel Apuya comments:

here is an example of implementation

[[LINK href="http://gravitywiz.com/customzing-multi-file-merge-tag/"]]
http://gravitywiz.com/customzing-multi-file-merge-tag/[[/LINK]]


Romel Apuya comments:

the whole code would be in the functions.php


<?php
/**
* Gravity Wiz // Multi-File Merge Tag for Post Content Templates
*
* Enhance the merge tag for multi-file upload fields by adding support for outputting markup that corresponds to the
* uploaded file. Example: image files will be wrapped in an <img> tag. Out of the box, this snippet only supports
* images and is limited to the 'jpg', 'png', and 'gif'.
*
* The default merge tag for the multi-file upload field will output the URL for each of the files.
*
* @version 1.1
* @author David Smith <[email protected]>
* @license GPL-2.0+
* @link http://gravitywiz.com/...
* @copyright 2013 Gravity Wiz
*/
class GW_Multi_File_Merge_Tag {

private static $instance = null;

/**
* Temporarily stores the values of the 'gform_merge_tag_filter' filter for use in the 'gform_replace_merge_tags' filter.
*
* @var array
*/
private $_merge_tag_args = array();
private $_settings = array();

private function __construct() {
add_filter( 'gform_merge_tag_filter', array( $this, 'filter_merge_tag' ), 10, 5 );
}

public static function get_instance() {

if( null == self::$instance )
self::$instance = new self;

return self::$instance;
}

public function get_default_args() {
return array(
'form_id' => false,
'exclude_forms' => array(),
'default_markup' => '<li><a href="{url}">{filename}.{ext}</a></li>',
'markup' => array(
array(
'file_types' => array( 'jpg', 'png', 'gif' ),
'markup' => '<img src="{url}" width="33%" />'
),
array(
'file_types' => array( 'mp4', 'ogg', 'webm' ),
'markup' => '<video width="320" height="240" controls>
<source src="{url}" type="video/{ext}">
Your browser does not support the video tag.
</video>'
),
array(
'file_types' => array( 'ogv' ),
'markup' => '<video width="320" height="240" controls>
<source src="{url}" type="video/ogg">
Your browser does not support the video tag.
</video>'
)
)
);
}

public function register_settings( $args = array() ) {

$args = wp_parse_args( $args, $this->get_default_args() );

if( ! $args['form_id'] ) {
$this->_settings['global'] = $args;
} else {
$this->_settings[$args['form_id']] = $args;
}

}

public function filter_merge_tag( $value, $input_id, $options, $field, $raw_value ) {

if( empty( $this->_settings ) )
return $value;

// only process for individual merge tags (no {all_fields})
if( $input_id != $field['id'] || ! $this->is_applicable_field( $field ) )
return $value;

$this->_merge_tag_args = compact( 'value', 'input_id', 'options', 'field', 'raw_value' );
$this->_merge_tag_args['placeholder'] = "{:{$input_id}:{$options}}";

add_filter( 'gform_replace_merge_tags', array( $this, 'replace_merge_tag' ), 10, 7 );

// replace merge tag to be replaced later
return $this->_merge_tag_args['placeholder'];
}

public function replace_merge_tag( $text, $form, $entry, $url_encode, $esc_html, $nl2br, $format ) {

if( ! $form ) {
return $text;
}

remove_filter( 'gform_replace_merge_tags', array( $this, 'replace_merge_tag' ) );

preg_match_all( '/{[^{]*?:(\d+(\.\d+)?)(:(.*?))?}/mi', $text, $matches, PREG_SET_ORDER );

foreach( $matches as $match ) {

$input_id = $match[1];
$field = GFFormsModel::get_field( $form, $input_id );

if( ! $this->is_applicable_field( $field ) )
continue;

if( $format != 'html' ) {

$value = $this->_merge_tag_args['value'];

} else {

$value = GFFormsModel::get_lead_field_value( $entry, $field );
$files = empty( $value ) ? array() : json_decode( $value, true );

$value = '';
foreach( $files as &$file ){
$value .= $this->get_file_markup( $file, $form['id'] );
}

}

$text = str_replace( $match[0], $value, $text );

}

return $text;
}

public function get_file_markup( $file, $form_id ) {

$value = str_replace( " ", "%20", $file );
$file_info = pathinfo( $value );

extract( $file_info ); // gives us $dirname, $basename, $extension, $filename

if( ! $extension )
return $value;

$markup_settings = $this->get_markup_settings( $form_id );
if( empty( $markup_settings ) )
return $value;

$markup_found = false;

foreach( $markup_settings as $file_type_markup ) {

$file_types = array_map( 'strtolower', $file_type_markup['file_types'] );
if( ! in_array( strtolower( $extension ), $file_types ) )
continue;

$markup_found = true;
$markup = $file_type_markup['markup'];

$tags = array(
'{url}' => $file,
'{filename}' => $filename,
'{basename}' => $basename,
'{ext}' => $extension
);

foreach( $tags as $tag => $tag_value ) {
$markup = str_replace( $tag, $tag_value, $markup );
}

$value = $markup;
break;
}

if( ! $markup_found && $default_markup = $this->get_default_markup( $form_id ) ) {

$tags = array(
'{url}' => $file,
'{filename}' => $filename,
'{basename}' => $basename,
'{ext}' => $extension
);

foreach( $tags as $tag => $tag_value ) {
$default_markup = str_replace( $tag, $tag_value, $default_markup );
}

$value = $default_markup;

}

return $value;
}

public function get_markup_settings( $form_id ) {

$form_markup_settings = rgars( $this->_settings, "$form_id/markup" ) ? rgars( $this->_settings, "$form_id/markup" ) : array();
$global_markup_settings = rgars( $this->_settings, 'global/markup' ) ? rgars( $this->_settings, 'global/markup' ) : array();

return array_merge( $form_markup_settings, $global_markup_settings );
}

public function get_default_markup( $form_id ) {

$default_markup = rgars( $this->_settings, "$form_id/default_markup" );
if( ! $default_markup )
$default_markup = rgars( $this->_settings, 'global/default_markup' );

return $default_markup;
}

public function is_excluded_form( $form_id ) {

$has_global_settings = isset( $this->_settings['global'] );
$excluded_forms = (array) rgars( $this->_settings, 'global/exclude_forms' );

$explicity_excluded = $has_global_settings && in_array( $form_id, $excluded_forms );
$passively_excluded = ! $has_global_settings && ! isset( $this->_settings[$form_id] );
return $explicity_excluded || $passively_excluded;
}
public function is_applicable_field( $field ) {
$is_valid_form = ! $this->is_excluded_form( $field['formId'] );
$is_file_upload_filed = GFFormsModel::get_input_type( $field ) == 'fileupload';
$is_multi = rgar( $field, 'multipleFiles' );
return $is_valid_form && $is_file_upload_filed && $is_multi;
}
}
function gw_multi_file_merge_tag() {
return GW_Multi_File_Merge_Tag::get_instance();
}

add_filter("gform_after_submission_10", "my_add_post_attachements",10,2); //Your form ID

function my_add_post_attachements($entry, $form){
gw_multi_file_merge_tag()->register_settings( array(
'form_id' => 10, //Your form ID
'markup' => array(
array(
'file_types' => array( 'jpg', 'jpeg', 'png', 'gif' ),
'markup' => '<div class="gw-image"><a href="{url}" class="gw-image-link"><img src="{url}" width="100%" /></a><span>{filename}</span></div>'
),
array(
'file_types' => array( 'mp4', 'ogg', 'webm' ),
'markup' => '<video width="320" height="240" controls>
<source src="{url}" type="video/{ext}">
Your browser does not support the video tag.
</video>'
)
)
) );
}


npeplow comments:

Hi Romel

Thanks for the link.

Can you please point out which part of that allows me to enter a title for the file? This is a key requirement for me.

Thanks
Nick


Romel Apuya comments:

Thats not currently in the code but we can add a way to add option for the filename..

here's a link on how to save the images..

[[LINK href="http://gravitywiz.com/rename-uploaded-files-for-gravity-form/"]]http://gravitywiz.com/rename-uploaded-files-for-gravity-form/[[/LINK]]


btw i can help you on your staging server.

can you add me on skype: rrapuya


cheers,

romel


Romel Apuya comments:

you can create a script which adds a textbox beside the uploaded file via jquery. save it using the second link i posted.