Home / functions.php / Shortcode for HTML5 audio in posts and pages
  • How to Shortcode for HTML5 audio in posts and pages


    Adding this snippet to the functions.php of your wordpress theme will create a new shortcode for HTML5 audio. Just add the second snippet of shortcode to a post or page to add HTML5 audio.

    ( click code to copy )

    wordpress snippet : PHP

    function html5_audio($atts, $content = null) {
    		"src" => '',
    		"autoplay" => '',
    		"preload"=> 'true',
    		"loop" => '',
    		"controls"=> ''
    	), $atts));
    	return '<audio src="'.$src.'" autoplay="'.$autoplay.'" preload="'.$preload.'" loop="'.$loop.'" controls="'.$controls.'" autobuffer />';
    add_shortcode('audio5', 'html5_audio');
    ( click code to copy )

    wordpress snippet : SHORTCODE

    [audio5 src="http://your-site/videos/your-video.mp4" loop="true" autoplay="autoplay" preload="auto" loop="loop" controls=""]

    ( WordPress codex functions, hooks, in this snippet. )

    add_shortcode, shortcode_atts,
  • Any ideas on how to use Flash as a fallback?

    • I would suggest that you use jplayer http://jplayer.org/ it will use flash if the browser does not support HTML5 audio. You will have to do some fiddling to get things running with wordpress though.