2017-03-29 9 views

答えて

1

WordPressには、これを実現するのに役立つID3 libraryを使用してオーディオ機能が組み込まれています。

まず、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_formatted']がフォーマットされた文字列で時刻を返しますながら$metadata['length']は時間を秒単位で返します。

注×2:フィールドは機能が実行されるように、あなたが$_POST['audio_file']get_field()機能を変更する必要がありますポストに保存される前に、あなたはこれを実行するアクションに「1」〜「20」に変更した場合ACFがフィールドをDBに保存する前に

関連する問題