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
Related
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!
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
}
After creating new post I need to generate and store the .vcf file in storage. I don't know how I can do this after post save. Didn't find help to develop such function. Please help!
Try this :
On "save_post" hook you can write function. That will create .vcf file with the name of post_title at specified directory.
function my_project_create_vCard( $post_id ) {
$vpost = get_post($post->ID);
$filename = strtolower(str_replace(" ","-",$vpost->post_title)).".vcf";
header('Content-type: text/x-vcard; charset=utf-8');
header("Content-Disposition: attachment; filename=".$filename);
$data=null;
$data.="BEGIN:VCARD\n";
$data.="VERSION:2.1\n";
$data.="FN:".$vpost->post_title."\n"; // get post title
$data.="EMAIL:" .get_field('email',$vpost->ID)."\n"; // get acf field value
$data.="END:VCARD";
$filePath = get_template_directory()."/".$filename; // you can specify path here where you want to store file.
$file = fopen($filePath,"w");
fwrite($file,$data);
fclose($file);
}
add_action( 'save_post', 'my_project_create_vCard' );
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/
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.