0

How to return html as a string from php for WordPress

function play_audio($atts){
    $a = shortcode_atts( array(
        'url' => ''
    ), $atts );
    wp_enqueue_script('player');
    wp_enqueue_script('play_script');
    wp_enqueue_style('progress_bar');
    return '<div class="mediPlayer">
    <audio class="listen" preload="none" data-size="250" src=" . $a[url] . "></audio>
</div>
</div>';
}
add_shortcode( 'playaudio', 'play_audio' );

src=" . $a[url] . "> I'm confuse with this part its not return url

1
  • 1
    There's nothing WordPress specific about returning strings. Strings are a basic feature of PHP and work the same everywhere. Oct 11, 2019 at 0:14

1 Answer 1

0

You are missing multiple single quotes around $a[url], so after src=" there should be a ' and after $a[url] there should be another single quote, and you also need to quote the array key, so the whole return line should be

return '<audio class="listen" preload="none" data-size="250" src="' . $a['url'] . '"></audio></div></div>';
1
  • its works, but plugin not working javascript error. Oct 12, 2019 at 10:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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