사용자 정의 필드를 통해 업로드 할 때 오디오 파일의 길이를 가져 와서 포스트 메타에 저장하고 싶습니다.
WordPress에는 이를 달성하는 데 도움이 되는 ID3 라이브러리 를 사용하는 오디오 기능이 내장되어 있습니다.
먼저 acf / save_post 후크를 사용하여 ACF에 연결합니다. 그런 다음 WP 함수 wp_read_audio_metadata () 를 사용하여 오디오 파일의 메타 데이터를 가져옵니다. 마지막으로 update_post_meta () 함수를 사용하여 데이터를 게시물에 저장합니다. 이 같은:
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']
은 시간을 초 단위로 $metadata['length_formatted']
반환하고는 형식이 지정된 문자열로 시간을 반환합니다.
참고 x2 : 필드가 게시물에 저장되기 전에이를 실행하는 작업에서 "20"을 "1"로 변경하면 ACF가 필드를 DB에 저장하기 전에 함수가 실행 되므로 get_field()
함수를 $_POST['audio_file']
로 변경해야합니다. .
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다