How to add audio files to wordpress blog and making it auto play?

Basic parts

To get all audio files attached to a post use get_children():

$audio_files = get_children(
    array (
        'post_parent'    => get_the_ID(),
        'post_status'    => 'inherit',
        'post_type'      => 'attachment',
        'post_mime_type' => 'audio'
    )
);

For the last attached file URL use wp_get_attachment_url() and pass the ID of the last child as an argument:

$id   = array_pop( array_keys( $audio_files ) );
$url  = wp_get_attachment_url( $id );

To insert an audio player use the HTML5 audio element with an autoplay attribute:

$audio = "<audio src="https://wordpress.stackexchange.com/questions/67108/$url" controls autoplay loop></audio>";

There are other solutions to play audio files with a better compatibility. I’ll leave it up to you to implement those.

The plugin

Let’s put this together. I recommend to use this a plugin so you can turn it off fast once your visitors start complaining. That will happen soon, see the caveats at the end of this answer. 🙂

<?php
/* Plugin Name: Auto play latest music attachment */

add_filter( 'the_content', 'wpse_67108_autplay_music' );

function wpse_67108_autplay_music( $content )
{
    if ( ! is_singular() )
    {
        return $content;
    }

    $audio_files = get_children(
        array (
            'post_parent'    => get_the_ID(),
            'post_status'    => 'inherit',
            'post_type'      => 'attachment',
            'post_mime_type' => 'audio'
        )
    );

    $audio = '';

    if ( $audio_files )
    {
        $id   = array_pop( array_keys( $audio_files ) );
        $url  = wp_get_attachment_url( $id );
        // add a 'controls' attribute to enable controls
        $audio = "<audio src="https://wordpress.stackexchange.com/questions/67108/$url" controls autoplay loop></audio>";
    }

    return $audio . $content;
}

screen shot of a sample post

Uploading audio files

By default WordPress does not allow uploading all files. A wav for example will be rejected. You might need another plugin to extend the list of allowed mime (file) types:

<?php
/* Plugin Name: Extend MIME types */
add_filter( 'upload_mimes', 't5_upload_mimes' );

if ( ! function_exists( 't5_upload_mimes' ) )
{
    function t5_upload_mimes( $mime_types = array() )
    {
        $mime_types['ai']  = 'application/postscript';
        $mime_types['psd'] = 'image/vnd.adobe.photoshop';
        $mime_types['svg'] = 'image/svg';
        $mime_types['tex'] = 'application/x-tex';
        $mime_types['tgz'] = 'application/x-compressed';
        $mime_types['tmd'] = 'application/octet-stream';
        // audio
        $mime_types['aac']  = 'audio/aac';
        $mime_types['mid']  = 'audio/mid';
        $mime_types['mp1']  = 'audio/mpeg';
        $mime_types['mp2']  = 'audio/mpeg';
        $mime_types['mp3']  = 'audio/mpeg';
        $mime_types['mpg']  = 'audio/mpeg';
        $mime_types['mpeg'] = 'audio/mpeg';
        $mime_types['mp4']  = 'audio/mp4';
        $mime_types['m4a']  = 'audio/mp4';
        $mime_types['oga']  = 'audio/ogg';
        $mime_types['ogg']  = 'audio/ogg';
        $mime_types['wav']  = 'audio/wav';
        $mime_types['webm'] = 'audio/webm';
        return $mime_types;
    }
}

Caveats

Please add a warning in your post list. Do not omit the controls. Many visitors will not enjoy auto play sounds because they …

  • are already listening to other music,
  • are in phone call,
  • work in an office together with other people,
  • use a screen reader,
  • have a chronic tinnitus that is triggered by some sounds (I am one of those).

Leave a Comment