Use ACF load_value hook to pre-populate the Upload File field - wordpress

I'm trying to pre-populate the File upload field with files already submitted previously, using ACF PRO and ACF Frontend Form.
So far, I am able to retrieve a list of files that was previously uploaded but can't seem to figure out a way to set it in the file upload frontend.
The File upload field lives inside a Repeater group.
In the functions.php:
I added a load_value with key set to the repeater group:
add_filter('acf/load_value/key=field_5e80ed8f4516b', 'my_acf_set_file_list', 10, 3);
Then in the function I attempted to set the $value to be an array of file urls.
'field_5e80edf14516c' is the key for the File field.
function my_acf_set_file_list( $value, $post_id, $field ){
$order_id = $_GET['order'];
$value = array();
// get existing files
$existing_files = get_post_meta( $order_id, 'file', true);
if ( !empty( $existing_files ) ) {
$i = 0;
foreach ( $existing_files as $file ) {
$value[$i]['field_5e80edf14516c'] = $file;
// also tried
// $value[$i]['field_5e80edf14516c'] = $file['file'];
}
}
return $value;
}
The problem is in the frontend form, there seems to be no change at all. No file was pre-populated.
I'm pretty sure I didn't get the array structure correct, but I've searched all over and cannot find any related information.
The post data for when I upload existing files seem to indicate the attachment ID is used. But I'm not sure what is the row-0 and other id 5ebf042bba99f?
acf[field_5e80ed8f4516b]:
acf[field_5e80ed8f4516b][row-0][field_5e80edf14516c]: 381
acf[field_5e80ed8f4516b][5ebf042bba99f][field_5e80edf14516c]: 381
acf[field_5e80ed8f4516b][5ebf042cba9a0][field_5e80edf14516c]: 401
Thanks in advance!

Related

Delete Gravity Forms file attachments after submission but keep all other fields?

OK, we have a job submission form on the site where users need to complete the form and attach resume and cover letter. For privacy reasons, we do not want to keep the resumes and cover letters on the server, so what we've been doing is send emails with form submission, attach resume and cover letter, and then completely delete the submission.
This is the code we've been using and everything works the way it should.
/**
*
* Target submissions from form ID 2.
*
* Change gform_after_submission_2 to reflect your target form ID,
* or use gform_after_submission to target all forms.
*
*/
add_action( 'gform_after_submission_2', 'remove_form_entry' );
function remove_form_entry( $entry ) {
GFAPI::delete_entry( $entry['id'] );
}
I was wondering if there's a way to keep the form entries but just delete the attachments?
I have just wrote the function, tested and it works fine to delete resume file.
You can add more code same way for cover letters.
add_filter( 'gform_after_submission_2', 'remove_form_entry', 10, 2 );
function remove_form_entry( $entry, $form ) {
global $wpdb;
$lead_id = $entry['id'];
$meta_key = 1; //it is the uploads field id.
$entry_table = $wpdb->prefix . 'gf_entry_meta'; //meta table of gravity forms
$get_resume_file_statement = $wpdb->prepare( "SELECT `meta_value` FROM `$entry_table` WHERE `meta_key` = %d AND `entry_id` = %d", $meta_key, $lead_id);
$meta_values_of_resume_file = $wpdb->get_col( $get_resume_file_statement ); // get uploaded csv file url
$resume_url = explode('uploads', $meta_values_of_resume_file [0]); //get file url after uploads folder. It returns file url like http://localhost/g2a/wp-content/uploads/gravity_forms/3-dbbe121585c30ed9e49ec2a6803270b0/2021/07/89636498_188586552577282_8867025936109797376_n.jpg
$resume_file_name = end($resume_url ); //get file url after uploads folder. value like /gravity_forms/3-dbbe121585c30ed9e49ec2a6803270b0/2021/07/89636498_188586552577282_8867025936109797376_n.jpg
$upload_dir = wp_upload_dir(); //uploads dir
$full_path = $upload_dir['basedir'] . $resume_file_name; //get full path of file
wp_delete_file( $full_path ); //delete the file
//update entry meta field value here or delete the entry meta.
}

Adding a Woocommerce Downloadable File From Custom Field

Using Advanced Custom Fields, I've added a "Post Object" field to my Woocommerce edit page, set to return Post IDs.
I'm trying to get data from these posts, and automatically populate my Woocommerce Downloadable Files from them.
The problem with my current code is that if I change the data in the custom field, and then save my product, it doesn't update the downloads. I need to update the product again for the downloads to be affected.
add_action( 'woocommerce_admin_process_product_object', 'mysite_add_downloads', 50, 1 );
function mysite_add_downloads( $product ){
$mysite_select_albums = get_field('mysite_select_albums');
$downloads = array(); // Clear $downloads variable
foreach ($mysite_select_albums as $album) {
// Gets Information About Download From Album Post Type
$post = get_post($album);
$file_title = $post->post_title;
$file_url = "http://changingthinstosimplify.com/";
$file_md5 = md5($file_url);
$download = new WC_Product_Download(); // Get an instance of the WC_Product_Download Object
// Set the download data
$download->set_name($file_title);
$download->set_id($file_md5);
$download->set_file($file_url);
$downloads[$file_md5] = $download; // Insert the new download to the array of downloads
}
$product->set_downloads($downloads); // Set new array of downloads
}

Get file duration uploaded from custom field wordpress

I want to get the duration of audio file when uploaded through custom field and save it in the post meta.
WordPress has built in audio functions using the ID3 library that will help you achieve this.
First you will hook into ACF using the acf/save_post hook. Then you will use the WP function wp_read_audio_metadata() to get the meta data of the audio file. Lastly you will use the update_post_meta() function to save the data to the post. Something like this:
function save_audio_duration($post_id) {
// Get the WP Uploads Directory (where ACF saves files)
$uploads = wp_upload_dir();
$uploads_dir = ( $uploads['baseurl'] . $uploads['subdir'] );
// Get the file name from ACF & create the file string
$file_obj = get_field('audio_file', $post_id);
$file = $uploads_dir . '/' . $file_obj['filename'];
// Use the wp_read_audio_metadata() function to get data
$metadata = wp_read_audio_metadata( $file );
// Save the file length to the post meta
update_post_meta($post_id, 'audio_length', $metadata['length']);
}
// Will execute AFTER post has been saved (change "20" to "1" to execute before)
add_action('acf/save_post', 'save_audio_duration', 20);
Note: $metadata['length'] will return the time in seconds while $metadata['length_formatted'] will return the time in a formatted string.
Note x2: If you change the "20" to "1" in the action to execute this BEFORE the fields are saved to the post you will need to change the get_field() function to $_POST['audio_file'] as the function will be executed before ACF saves the fields to the DB.
I change a little your good code for vĂ­deos:
function save_video_duration($post_id) {
// Get the file name from ACF & create the file string
$file_obj = get_field('video_file', $post_id);
// Get the WP Uploads Directory (where ACF saves files)
$file = get_attached_file( attachment_url_to_postid( get_field('video_file', $post_id) ) );
// Use the wp_read_audio_metadata() function to get data
$metadata = wp_read_video_metadata($file);
// Save the file length to the post meta
update_post_meta($post_id, 'video_file_length', $metadata['length']);
}
// Will execute AFTER post has been saved (change "20" to "1" to execute before)
add_action('acf/save_post', 'save_video_duration', 20);
Thanks

How to modify uploaded file URL in Gravity Forms using "gform_save_field_value" filter

I want to modify the uploaded file URL before saving to database when user submits the Gravity form.
I am trying to acheive this by using "gform_save_field_value" filter in Gravity form. But when i print fields inside the filter function i never get the file upload field ID. All other fields are accessable inside filter function except file upload input field.
add_filter( 'gform_save_field_value', 'save_field_value', 10, 4 );
function save_field_value( $value, $lead, $field, $form ) {
print_r($field);
}
The gform_upload_path filter will allow you to change the upload path of the file (and the URL).
add_filter( 'gform_upload_path', 'change_upload_path', 10, 2 );
function change_upload_path( $path_info, $form_id ) {
$path_info['path'] = '/home/public_html/yourdomainfolder/new/path/';
$path_info['url'] = 'http://yourdomainhere.com/new/path/';
return $path_info;
}
If you need to change the actual name of the file, here is a snippet that makes it a cinch.
http://gravitywiz.com/rename-uploaded-files-for-gravity-form/

Change wordpress gallery shortcode slug to filename

Is it possible to change the attachment page slug to the referring filename? In short... i use the Gallery Shortcode to build a simple page based gallery.
I change the orignal filename (like DSC1223.jpg) during the upload process (to 3b1871561aab.jpg) but it does not appery as slug within the url. It uses only DSC1223.
Is there anyway to change ist?
Regards, Steve
The best way would be to write something like this in my functions.php
function twentyten_filter_wp_handle_upload () {
$upload = wp_handle_upload();
}
add_filter( 'wp_handle_upload', 'twentyten_filter_wp_handle_upload', 10, 2);
Add this to the hash upload filename plugin and you should be good to go;
/**
* Filter new attachments and set the post_name the same as the hashed
* filename.
*
* #param int $post_ID
*/
function change_attachment_name_to_hash($post_ID)
{
$file = get_attached_file($post_ID);
$info = pathinfo($file);
$name = trim( substr($info['basename'], 0, -(1 + strlen($info['extension'])) ) );
wp_update_post(array(
'ID' => $post_ID,
'post_name' => $name
));
}
add_action('add_attachment', 'change_attachment_name_to_hash');
Don't hesitate to ask if you're not sure what each line does!
UPDATE:
This function hooks onto the add_attachment event, just after a new attachment is saved to the database. This action is called from within wp_insert_attachment().
We first grab the filename of the attachment (get_attached_file()). Then we use a native PHP function pathinfo() to get the path components, and strip the directory path and file extension away.
Then we call wp_update_post(), updating the post_name of the attachment in the database.

Resources