获取从自定义字段 wordpress 上传的文件持续时间
Get file duration uploaded from custom field wordpress
我想在通过自定义字段上传时获取音频文件的持续时间并将其保存在 post 元中。
WordPress 使用 ID3 library 内置了音频功能,可以帮助您实现这一目标。
首先,您将使用 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() 函数连接到 ACF 以将数据保存到 post。像这样:
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);
注意: $metadata['length']
将 return 以秒为单位的时间,而 $metadata['length_formatted']
将 return 格式化字符串中的时间.
注意 x2: 如果您在将字段保存到 post 之前将操作中的“20”更改为“1”,您将需要将 get_field()
函数更改为 $_POST['audio_file']
,因为该函数将在 ACF 将字段保存到数据库之前执行。
我稍微修改一下你的视频代码:
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);
谢谢
我想在通过自定义字段上传时获取音频文件的持续时间并将其保存在 post 元中。
WordPress 使用 ID3 library 内置了音频功能,可以帮助您实现这一目标。
首先,您将使用 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() 函数连接到 ACF 以将数据保存到 post。像这样:
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);
注意: $metadata['length']
将 return 以秒为单位的时间,而 $metadata['length_formatted']
将 return 格式化字符串中的时间.
注意 x2: 如果您在将字段保存到 post 之前将操作中的“20”更改为“1”,您将需要将 get_field()
函数更改为 $_POST['audio_file']
,因为该函数将在 ACF 将字段保存到数据库之前执行。
我稍微修改一下你的视频代码:
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);
谢谢