Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

How to add audio files to wordpress blog that when someone clicks on a post Music began playing by itself.

share|improve this question
    
Welcome to our site. What have you tried already? –  toscho Oct 4 '12 at 11:39
4  
Hi there that is an HTML or issue with your audio player and making it auto play. BTW Auto Play music is considered a very bad user experience. –  Damien Oct 4 '12 at 11:52
    
@toscho thank you i've tried [ audio URL ] but this gives download link, this is not what I want, i want when someone clicks on a post Music began playing by itself. –  Neo Oct 4 '12 at 11:55
add comment

1 Answer

up vote 3 down vote accepted

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='$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='$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).
share|improve this answer
    
oh yes, yes, of course! ;) –  Neo Oct 6 '12 at 14:46
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.