Using wp_read_audio_metadata ()

I am trying to get some metadata from an mp3 file in WordPress. In particular, variable length. Here is some of my code. It is not shown here, but I have included the wp-admin / includes / media.php file. When I look at my page http://beta.openskyministry.org/podcasts/ I just see empty tags for<itunes:length></itunes:length>

Let me know if you need anything else to answer my question.

$aud_meta = wp_read_audio_metadata($aud_url); ?>

    <item>


        <title><?php the_title(); ?></title>

        <itunes:author><?php echo htmlspecialchars((get_bloginfo('name'))); ?></itunes:author>

        <itunes:summary><?php echo  htmlspecialchars(strip_tags(get_the_excerpt())); ?></itunes:summary> 

        <itunes:length><?php echo $aud_meta['length_formatted']; ?></itunes:length>

      

+3


source to share


1 answer


WordPress already stores media metadata so there is no need to iterate over it. The solution is as simple as:

add_action( 'wp_head', function(){
    global $post;
    if ( is_single($post) ) {
        $args = array( 
            'post_type'     => 'attachment',
            'numberposts'   => 1,
            'post_parent'   => $post->ID,
            'post_mime_type' => 'audio'
        );  
        $attachments = get_posts( $args );
        if($attachments){
            $meta = wp_get_attachment_metadata( $attachments[0]->ID );
            echo "<itunes:length>{$meta['length_formatted']}</itunes:length>";
        }           
    }
});

      

For entries, wp_read_audio_metadata()

expect a file path, not a URL. If necessary, this should be:



$path = get_attached_file( $attachment->ID );
$meta = wp_read_audio_metadata($path);
echo "<itunes:length>{$meta['length_formatted']}</itunes:length>";

      

Related : Save camera information as metadata when uploading image?

+1


source







All Articles