1

I'm almost finished the re-skin and development of a customer site, and one of the final items I need to build support for is the audio shortcode they're using. The problem I'm having is that the audio shortcode does not use an attribute, but has an attribute value:

[audio=http://1234.domain.com/wp-content/uploads/audiofile.mp3]

How do I render this on the page?

I would use the following function, but there is no attribute in the above shortcode:

function audio_shortcode($atts) {
    $a = shortcode_atts( array(
        'uri' => ''
    ), $atts);

    return '<audio controls><source src="' . $a['uri'] . '" type="audio/mpeg"></audio>';
}
add_shortcode('mp3audio', 'audio_shortcode');

I reviewed the documentation here: https://codex.wordpress.org/Shortcode_API but it doesn't have any examples here [scode="value"].

Any suggestions on how to proceed?

2

When no attribute is used but a value is provided, the value is added to $atts[0]. Using $atts[0], I'm now able to get the value output doing the following:

function audio_shortcode($atts) {
    if($atts[0] != '') {
        return '
            <div class="audiofile">
                <audio controls>
                    <source src="' . $atts[0] . '" type="audio/mpeg">
                </audio>
            </div>';
    }
}
add_shortcode('audio', 'audio_shortcode');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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