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
  • 2
    This might help: wordpress.stackexchange.com/questions/39027/… Commented Jun 26, 2018 at 13:39
  • Thank you cjbj. It did help. Unfortunate there wasn't more information about this use case anywhere. Commented Jun 26, 2018 at 16:21

1 Answer 1

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 and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.